Skip to content

Commit b17a1cd

Browse files
committed
Implement FileContentResultAssertions
1 parent 856e37c commit b17a1cd

File tree

9 files changed

+187
-6
lines changed

9 files changed

+187
-6
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ public FileResultAssertions BeFileResult(string reason, params object[] reasonAr
107107
return new FileResultAssertions(Subject as FileResult);
108108
}
109109

110+
/// <summary>
111+
/// Asserts that the subject is an <see cref="FileResult"/>.
112+
/// </summary>
113+
public FileContentResultAssertions BeFileContentResult()
114+
{
115+
return BeFileContentResult(string.Empty, null);
116+
}
117+
118+
/// <summary>
119+
/// Asserts that the subject is an <see cref="FileResult"/>.
120+
/// </summary>
121+
public FileContentResultAssertions BeFileContentResult(string reason, params object[] reasonArgs)
122+
{
123+
Execute.Assertion
124+
.BecauseOf(reason, reasonArgs)
125+
.ForCondition(Subject is FileContentResult)
126+
.FailWith(Constants.CommonFailMessage, typeof(FileContentResult).Name, Subject.GetType().Name);
127+
128+
return new FileContentResultAssertions(Subject as FileContentResult);
129+
}
130+
110131
/// <summary>
111132
/// Asserts that the subject is an <see cref="JsonResult"/>.
112133
/// </summary>

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
<data name="CommonTypeFailMessage" xml:space="preserve">
127127
<value>Expected {0} to be of type '{1}' but was '{2}'</value>
128128
</data>
129+
<data name="FileContentResult_WithFileContents_LengthFail" xml:space="preserve">
130+
<value>Expected "FileContentResult.FileContents" to have {0} byte(s), but found {1}.</value>
131+
</data>
132+
<data name="FileContentResult_WithFileContents_MatchFail" xml:space="preserve">
133+
<value>Expected "FileContentResult.FileContents[{0}]" to be {1:x2}, but found {2:x2}.</value>
134+
</data>
129135
<data name="RedirectToActionResult_RouteValues_ContainsKey" xml:space="preserve">
130136
<value>RedirectToActionResult.RouteValues does not contain key {0}.</value>
131137
</data>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using FluentAssertions.Execution;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Diagnostics;
4+
5+
namespace FluentAssertions.AspNetCore.Mvc
6+
{
7+
/// <summary>
8+
/// Contains a number of methods to assert that a <see cref="FileContentResult" /> is in the expected state.
9+
/// </summary>>
10+
[DebuggerNonUserCode]
11+
public class FileContentResultAssertions : FileResultAssertions
12+
{
13+
#region Public Constructors
14+
15+
public FileContentResultAssertions(FileContentResult fileResult)
16+
: base(fileResult)
17+
{
18+
}
19+
20+
#endregion
21+
22+
#region Private Properties
23+
24+
private FileContentResult FileContentResultSubject => (FileContentResult)Subject;
25+
26+
#endregion Private Properties
27+
28+
#region Public Methods
29+
30+
/// <summary>
31+
/// Asserts that the file contents is the expected file contents.
32+
/// </summary>
33+
/// <param name="expectedFileContents">The expected file contents.</param>
34+
/// <param name="reason">
35+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
36+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
37+
/// </param>
38+
/// <param name="reasonArgs">
39+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
40+
/// </param>
41+
internal FileContentResultAssertions WithFileContents(byte[] expectedFileContents, string reason = "",
42+
params object[] reasonArgs)
43+
{
44+
var actualFileContents = FileContentResultSubject.FileContents;
45+
46+
Execute.Assertion
47+
.ForCondition(expectedFileContents.Length == actualFileContents.Length)
48+
.BecauseOf(reason, reasonArgs)
49+
.FailWith(FailureMessages.FileContentResult_WithFileContents_LengthFail, expectedFileContents.Length, actualFileContents.Length);
50+
for (int i = 0; i < expectedFileContents.Length; i++)
51+
{
52+
var expectedByte = expectedFileContents[i];
53+
var actualByte = actualFileContents[i];
54+
Execute.Assertion
55+
.ForCondition(expectedByte == actualByte)
56+
.BecauseOf(reason, reasonArgs)
57+
.FailWith(FailureMessages.FileContentResult_WithFileContents_MatchFail, i, expectedByte, actualByte);
58+
}
59+
60+
return this;
61+
}
62+
63+
#endregion
64+
}
65+
}

src/FluentAssertions.AspNetCore.Mvc/FileResultAssertions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@ namespace FluentAssertions.AspNetCore.Mvc
1313
[DebuggerNonUserCode]
1414
public class FileResultAssertions : ObjectAssertions
1515
{
16+
#region Public Constructors
17+
1618
public FileResultAssertions(FileResult fileResult)
1719
: base(fileResult)
1820
{
1921
}
2022

23+
#endregion
24+
2125
#region Private Properties
2226

2327
private FileResult FileResultSubject => (FileResult)Subject;
2428

2529
#endregion Private Properties
2630

31+
#region Public Methods
32+
2733
/// <summary>
2834
/// Asserts that the content type is the expected content type.
2935
/// </summary>
@@ -115,5 +121,7 @@ public FileResultAssertions WithLastModified(DateTimeOffset expectedLastModified
115121
.FailWith(FailureMessages.CommonFailMessage, "FileResult.LastModified", expectedLastModified, actualLastModified);
116122
return this;
117123
}
124+
125+
#endregion
118126
}
119127
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ public void BeFileResult_GivenNotFileResult_ShouldFail()
6464
.WithMessage("Expected ActionResult to be \"FileResult\", but found \"ViewResult\"");
6565
}
6666

67+
[Fact]
68+
public void BeFileContentResult_GivenFileContentResult_ShouldPass()
69+
{
70+
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain");
71+
72+
result.Should()
73+
.BeFileContentResult();
74+
}
75+
76+
[Fact]
77+
public void BeFileContentResult_GivenNotFileContentResult_ShouldFail()
78+
{
79+
ActionResult result = new ViewResult();
80+
Action a = () => result.Should().BeFileContentResult();
81+
82+
a.Should().Throw<Exception>()
83+
.WithMessage("Expected ActionResult to be \"FileContentResult\", but found \"ViewResult\"");
84+
}
6785

6886
[Fact]
6987
public void BeJson_GivenJson_ShouldPass()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Text;
4+
using Xunit;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc.Tests
7+
{
8+
public class FileContentResultAssertions_Tests
9+
{
10+
[Fact]
11+
public void WithContentType_GivenExpectedValue_ShouldPass()
12+
{
13+
var actualBytes = Encoding.ASCII.GetBytes("Test 1");
14+
var expectedBytes = Encoding.ASCII.GetBytes("Test 1");
15+
ActionResult result = new FileContentResult(actualBytes, "text/plain");
16+
17+
result.Should()
18+
.BeFileContentResult()
19+
.WithFileContents(expectedBytes);
20+
}
21+
22+
[Theory]
23+
[InlineData(
24+
"Test 1", "Test 11"
25+
, "Expected \"FileContentResult.FileContents\" to have 7 byte(s), but found 6.")]
26+
[InlineData(
27+
"Test 1a", "Test 2a"
28+
, "Expected \"FileContentResult.FileContents[5]\" to be 0x32, but found 0x31.")]
29+
public void WithContentType_GivenUnexpectedValue_ShouldFail(
30+
string actual, string expected, string failureMessage)
31+
{
32+
var actualBytes = actual != null ? Encoding.ASCII.GetBytes(actual) : null;
33+
var expectedBytes = expected != null ? Encoding.ASCII.GetBytes(expected) : null;
34+
ActionResult result = new FileContentResult(actualBytes, "text/plain");
35+
36+
Action a = () => result.Should()
37+
.BeFileContentResult()
38+
.WithFileContents(expectedBytes);
39+
40+
a.Should().Throw<Exception>()
41+
.WithMessage(failureMessage);
42+
}
43+
44+
}
45+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace FluentAssertions.AspNetCore.Mvc.Tests
1111
public class FileResultAssertions_Tests
1212
{
1313
[Fact]
14-
public void WithContentType_GivenValue_ShouldPass()
14+
public void WithContentType_GivenExpectedValue_ShouldPass()
1515
{
1616
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain");
1717

@@ -33,7 +33,7 @@ public void WithContentType_GivenUnexpected_ShouldFail()
3333
}
3434

3535
[Fact]
36-
public void WithFileDownloadName_GivenValue_ShouldPass()
36+
public void WithFileDownloadName_GivenExpectedValue_ShouldPass()
3737
{
3838
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain")
3939
{
@@ -61,7 +61,7 @@ public void WithFileDownloadName_GivenUnexpected_ShouldFail()
6161
}
6262

6363
[Fact]
64-
public void WithLastModified_GivenValue_ShouldPass()
64+
public void WithLastModified_GivenExpectedValue_ShouldPass()
6565
{
6666
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain")
6767
{
@@ -90,7 +90,7 @@ public void WithLastModified_GivenUnexpected_ShouldFail()
9090

9191

9292
[Fact]
93-
public void WithEntityTag_GivenValue_ShouldPass()
93+
public void WithEntityTag_GivenExpectedValue_ShouldPass()
9494
{
9595
var actualValue = new EntityTagHeaderValue("\"sha256 value 1\"");
9696
var expectedValue = new EntityTagHeaderValue("\"sha256 value 1\"");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc.Tests
99
public class JsonResultAssertions_Tests
1010
{
1111
[Fact]
12-
public void WithContentType_GivenValue_ShouldPass()
12+
public void WithContentType_GivenExpectedValue_ShouldPass()
1313
{
1414
ActionResult result = new JsonResult("value")
1515
{
@@ -37,7 +37,7 @@ public void WithContentType_GivenUnexpected_ShouldFail()
3737
}
3838

3939
[Fact]
40-
public void WithStatusCode_GivenValue_ShouldPass()
40+
public void WithStatusCode_GivenExpectedValue_ShouldPass()
4141
{
4242
ActionResult result = new JsonResult("value")
4343
{

0 commit comments

Comments
 (0)