Skip to content

Commit 61887bc

Browse files
committed
Implement VirtualFileResultAssertions
1 parent f7adf18 commit 61887bc

File tree

6 files changed

+185
-17
lines changed

6 files changed

+185
-17
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

+44-16
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ internal FileStreamResultAssertions BeFileStreamResult(string reason, params obj
173173
}
174174

175175
/// <summary>
176-
/// Asserts that the subject is an <see cref="JsonResult"/>.
176+
/// Asserts that the subject is an <see cref="PhysicalFileResult"/>.
177177
/// </summary>
178-
public JsonResultAssertions BeJsonResult()
178+
internal PhysicalFileResultAssertions BePhysicalFileResult()
179179
{
180-
return BeJsonResult(string.Empty, null);
180+
return BePhysicalFileResult(string.Empty, null);
181181
}
182182

183183
/// <summary>
184-
/// Asserts that the subject is an <see cref="JsonResult"/>.
184+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
185185
/// </summary>
186186
/// <param name="reason">
187187
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
@@ -190,26 +190,26 @@ public JsonResultAssertions BeJsonResult()
190190
/// <param name="reasonArgs">
191191
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
192192
/// </param>
193-
public JsonResultAssertions BeJsonResult(string reason, params object[] reasonArgs)
193+
internal PhysicalFileResultAssertions BePhysicalFileResult(string reason, params object[] reasonArgs)
194194
{
195195
Execute.Assertion
196196
.BecauseOf(reason, reasonArgs)
197-
.ForCondition(Subject is JsonResult)
198-
.FailWith(Constants.CommonFailMessage, typeof(JsonResult).Name, Subject.GetType().Name);
197+
.ForCondition(Subject is PhysicalFileResult)
198+
.FailWith(Constants.CommonFailMessage, typeof(PhysicalFileResult).Name, Subject.GetType().Name);
199199

200-
return new JsonResultAssertions(Subject as JsonResult);
200+
return new PhysicalFileResultAssertions(Subject as PhysicalFileResult);
201201
}
202202

203203
/// <summary>
204-
/// Asserts that the subject is an <see cref="PhysicalFileResult"/>.
204+
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
205205
/// </summary>
206-
internal PhysicalFileResultAssertions BePhysicalFileResult()
206+
internal VirtualFileResultAssertions BeVirtualFileResult()
207207
{
208-
return BePhysicalFileResult(string.Empty, null);
208+
return BeVirtualFileResult(string.Empty, null);
209209
}
210210

211211
/// <summary>
212-
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
212+
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
213213
/// </summary>
214214
/// <param name="reason">
215215
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
@@ -218,14 +218,42 @@ internal PhysicalFileResultAssertions BePhysicalFileResult()
218218
/// <param name="reasonArgs">
219219
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
220220
/// </param>
221-
internal PhysicalFileResultAssertions BePhysicalFileResult(string reason, params object[] reasonArgs)
221+
internal VirtualFileResultAssertions BeVirtualFileResult(string reason, params object[] reasonArgs)
222222
{
223223
Execute.Assertion
224224
.BecauseOf(reason, reasonArgs)
225-
.ForCondition(Subject is PhysicalFileResult)
226-
.FailWith(Constants.CommonFailMessage, typeof(PhysicalFileResult).Name, Subject.GetType().Name);
225+
.ForCondition(Subject is VirtualFileResult)
226+
.FailWith(Constants.CommonFailMessage, typeof(VirtualFileResult).Name, Subject.GetType().Name);
227227

228-
return new PhysicalFileResultAssertions(Subject as PhysicalFileResult);
228+
return new VirtualFileResultAssertions(Subject as VirtualFileResult);
229+
}
230+
231+
/// <summary>
232+
/// Asserts that the subject is an <see cref="JsonResult"/>.
233+
/// </summary>
234+
public JsonResultAssertions BeJsonResult()
235+
{
236+
return BeJsonResult(string.Empty, null);
237+
}
238+
239+
/// <summary>
240+
/// Asserts that the subject is an <see cref="JsonResult"/>.
241+
/// </summary>
242+
/// <param name="reason">
243+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
244+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
245+
/// </param>
246+
/// <param name="reasonArgs">
247+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
248+
/// </param>
249+
public JsonResultAssertions BeJsonResult(string reason, params object[] reasonArgs)
250+
{
251+
Execute.Assertion
252+
.BecauseOf(reason, reasonArgs)
253+
.ForCondition(Subject is JsonResult)
254+
.FailWith(Constants.CommonFailMessage, typeof(JsonResult).Name, Subject.GetType().Name);
255+
256+
return new JsonResultAssertions(Subject as JsonResult);
229257
}
230258

231259
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using FluentAssertions.Execution;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using System.Diagnostics;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc
7+
{
8+
/// <summary>
9+
/// Contains a number of methods to assert that a <see cref="VirtualFileResult" /> is in the expected state.
10+
/// </summary>
11+
[DebuggerNonUserCode]
12+
public class VirtualFileResultAssertions : FileResultAssertions
13+
{
14+
#region Public Constructors
15+
16+
public VirtualFileResultAssertions(VirtualFileResult fileResult)
17+
: base(fileResult)
18+
{
19+
}
20+
21+
#endregion
22+
23+
#region Public Properties
24+
25+
/// <summary>
26+
/// The <see cref="VirtualFileResult.FileName">FileName</see> on the <see cref="VirtualFileResult"/>
27+
/// </summary>
28+
public string FileName => VirtualFileResultSubject.FileName;
29+
30+
#endregion Private Properties
31+
32+
#region Private Properties
33+
34+
private VirtualFileResult VirtualFileResultSubject => (VirtualFileResult)Subject;
35+
36+
#endregion Private Properties
37+
38+
#region Public Methods
39+
40+
/// <summary>
41+
/// Asserts that the file name is the expected string.
42+
/// </summary>
43+
/// <param name="expectedFileName">The expected file name.</param>
44+
/// <param name="reason">
45+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
46+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
47+
/// </param>
48+
/// <param name="reasonArgs">
49+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
50+
/// </param>
51+
public FileResultAssertions WithFileName(string expectedFileName, string reason = "",
52+
params object[] reasonArgs)
53+
{
54+
var actualFileName = VirtualFileResultSubject.FileName;
55+
56+
Execute.Assertion
57+
.ForCondition(string.Equals(expectedFileName, actualFileName, StringComparison.OrdinalIgnoreCase))
58+
.BecauseOf(reason, reasonArgs)
59+
.FailWith(FailureMessages.CommonFailMessage, "VirtualFileResult.FileName", expectedFileName, actualFileName);
60+
return this;
61+
}
62+
63+
#endregion
64+
}
65+
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public void BePhysicalFileResult_GivenNotPhysicalFileResult_ShouldFail()
122122
.WithMessage("Expected ActionResult to be \"PhysicalFileResult\", but found \"ViewResult\"");
123123
}
124124

125+
[Fact]
126+
public void BeVirtualFileResult_GivenVirtualFileResult_ShouldPass()
127+
{
128+
ActionResult result = TestDataGenerator.CreateVirtualFileResult();
129+
130+
result.Should()
131+
.BeVirtualFileResult();
132+
}
133+
134+
[Fact]
135+
public void BeVirtualFileResult_GivenNotVirtualFileResult_ShouldFail()
136+
{
137+
ActionResult result = new ViewResult();
138+
Action a = () => result.Should().BeVirtualFileResult();
139+
140+
a.Should().Throw<Exception>()
141+
.WithMessage("Expected ActionResult to be \"VirtualFileResult\", but found \"ViewResult\"");
142+
}
143+
144+
125145
[Fact]
126146
public void BeJson_GivenJson_ShouldPass()
127147
{

tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/TestDataGenerator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static PhysicalFileResult CreatePhysicalFileResult(string fileName = "c:\
2929
return new PhysicalFileResult(fileName, "text/plain");
3030
}
3131

32+
public static VirtualFileResult CreateVirtualFileResult(string fileName = "~/temp.txt")
33+
{
34+
return new VirtualFileResult(fileName, "text/plain");
35+
}
36+
3237
public static FileStreamResult CreateFileStreamResult(Stream stream)
3338
{
3439
return new FileStreamResult(stream, "text/plain");

tests/FluentAssertions.AspNetCore.Mvc.Tests/PhysicalFileResultAssertions_Tests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void WithFileName_GivenUnexpectedValue_ShouldFail()
3737
}
3838

3939
[Fact]
40-
public void FileStream_GivenFileStreamResult_ShouldHaveTheSameStream()
40+
public void FileName_GivenPhysicalFileResult_ShouldHaveTheFileName()
4141
{
4242
var result = TestDataGenerator.CreatePhysicalFileResult();
4343

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using FluentAssertions.AspNetCore.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using Xunit;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc.Tests
7+
{
8+
public class VirtualFileResultAssertions_Tests
9+
{
10+
11+
[Fact]
12+
public void WithFileName_GivenExpectedValue_ShouldPass()
13+
{
14+
var actualFileName = "Test1.txt";
15+
var expectedFileName = string.Copy(actualFileName);
16+
ActionResult result = TestDataGenerator.CreateVirtualFileResult(actualFileName);
17+
18+
result.Should()
19+
.BeVirtualFileResult()
20+
.WithFileName(expectedFileName);
21+
}
22+
23+
[Fact]
24+
public void WithFileName_GivenUnexpectedValue_ShouldFail()
25+
{
26+
string actualFileName = "Test1.txt";
27+
string expectedFileName = "Test2.txt";
28+
ActionResult result = TestDataGenerator.CreateVirtualFileResult(actualFileName);
29+
var failureMessage = "Expected \"VirtualFileResult.FileName\" to be '\"Test2.txt\"' but found '\"Test1.txt\"'";
30+
31+
Action a = () => result.Should()
32+
.BeVirtualFileResult()
33+
.WithFileName(expectedFileName);
34+
35+
a.Should().Throw<Exception>()
36+
.WithMessage(failureMessage);
37+
}
38+
39+
[Fact]
40+
public void FileName_GivenVirtualFileResult_ShouldHaveTheFileName()
41+
{
42+
var result = TestDataGenerator.CreateVirtualFileResult();
43+
44+
result.Should()
45+
.BeVirtualFileResult()
46+
.FileName
47+
.Should().BeSameAs(result.FileName);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)