Skip to content

Commit 48dd387

Browse files
committed
Implement FileStreamResultAssertions. (+ nicer documentation comments)
1 parent b17a1cd commit 48dd387

File tree

8 files changed

+186
-15
lines changed

8 files changed

+186
-15
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FluentAssertions.Execution;
22
using FluentAssertions.Primitives;
33
using Microsoft.AspNetCore.Mvc;
4+
using System;
45
using System.Diagnostics;
56

67
namespace FluentAssertions.AspNetCore.Mvc
@@ -108,15 +109,15 @@ public FileResultAssertions BeFileResult(string reason, params object[] reasonAr
108109
}
109110

110111
/// <summary>
111-
/// Asserts that the subject is an <see cref="FileResult"/>.
112+
/// Asserts that the subject is an <see cref="FileContentResult"/>.
112113
/// </summary>
113114
public FileContentResultAssertions BeFileContentResult()
114115
{
115116
return BeFileContentResult(string.Empty, null);
116117
}
117118

118119
/// <summary>
119-
/// Asserts that the subject is an <see cref="FileResult"/>.
120+
/// Asserts that the subject is an <see cref="FileContentResult"/>.
120121
/// </summary>
121122
public FileContentResultAssertions BeFileContentResult(string reason, params object[] reasonArgs)
122123
{
@@ -128,6 +129,28 @@ public FileContentResultAssertions BeFileContentResult(string reason, params obj
128129
return new FileContentResultAssertions(Subject as FileContentResult);
129130
}
130131

132+
/// <summary>
133+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
134+
/// </summary>
135+
internal FileStreamResultAssertions BeFileStreamResult()
136+
{
137+
return BeFileStreamResult(string.Empty, null);
138+
139+
}
140+
141+
/// <summary>
142+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
143+
/// </summary>
144+
internal FileStreamResultAssertions BeFileStreamResult(string reason, params object[] reasonArgs)
145+
{
146+
Execute.Assertion
147+
.BecauseOf(reason, reasonArgs)
148+
.ForCondition(Subject is FileStreamResult)
149+
.FailWith(Constants.CommonFailMessage, typeof(FileStreamResult).Name, Subject.GetType().Name);
150+
151+
return new FileStreamResultAssertions(Subject as FileStreamResult);
152+
}
153+
131154
/// <summary>
132155
/// Asserts that the subject is an <see cref="JsonResult"/>.
133156
/// </summary>

src/FluentAssertions.AspNetCore.Mvc/FileContentResultAssertions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FluentAssertions.Execution;
22
using Microsoft.AspNetCore.Mvc;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace FluentAssertions.AspNetCore.Mvc
67
{
@@ -19,6 +20,17 @@ public FileContentResultAssertions(FileContentResult fileResult)
1920

2021
#endregion
2122

23+
#region Public Properties
24+
25+
/// <summary>
26+
/// The <see cref="FileContentResult.FileContents">FileContents</see> on the <see cref="FileContentResult"/>.
27+
/// </summary>
28+
[SuppressMessage("Performance", "CA1819:Properties should not return arrays",
29+
Justification = "It needs to return the same instance as FileContentResult")]
30+
public byte[] FileContents => FileContentResultSubject.FileContents;
31+
32+
#endregion Private Properties
33+
2234
#region Private Properties
2335

2436
private FileContentResult FileContentResultSubject => (FileContentResult)Subject;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using FluentAssertions.Execution;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Diagnostics;
4+
using System.IO;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc
7+
{
8+
/// <summary>
9+
/// Contains a number of methods to assert that a <see cref="FileStreamResult" /> is in the expected state.
10+
/// </summary>>
11+
[DebuggerNonUserCode]
12+
public class FileStreamResultAssertions : FileResultAssertions
13+
{
14+
#region Public Constructors
15+
16+
public FileStreamResultAssertions(FileStreamResult fileResult)
17+
: base(fileResult)
18+
{
19+
}
20+
21+
#endregion
22+
23+
#region Public Properties
24+
25+
/// <summary>
26+
/// The <see cref="FileStreamResult.FileStream">FileStream</see> on the <see cref="FileStreamResult"/>
27+
/// </summary>
28+
public Stream FileStream => FileStreamResultSubject.FileStream;
29+
30+
#endregion
31+
32+
#region Private Properties
33+
34+
private FileStreamResult FileStreamResultSubject => (FileStreamResult)Subject;
35+
36+
#endregion Private Properties
37+
38+
}
39+
}

src/FluentAssertions.AspNetCore.Mvc/JsonResultAssertions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public JsonResultAssertions(JsonResult subject) : base(subject)
2929
#region Public Properties
3030

3131
/// <summary>
32-
/// The serializer settings of the JsonResult.
32+
/// The <see cref="JsonResult.SerializerSettings"/> on the <see cref="JsonResult"/>.
3333
/// </summary>
3434
public JsonSerializerSettings SerializerSettings => JsonResultSubject.SerializerSettings;
3535

3636
/// <summary>
37-
/// The value on the JsonResult
37+
/// The <see cref="JsonResult.Value">Value</see> on the <see cref="JsonResult"/>.
3838
/// </summary>
3939
public object Value => JsonResultSubject.Value;
4040

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using FluentAssertions.AspNetCore.Mvc.Tests.Helpers;
12
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.AspNetCore.Routing;
34
using System;
@@ -48,7 +49,7 @@ public void BeEmpty_GivenNotEmpty_ShouldPass()
4849
[Fact]
4950
public void BeFileResult_GivenFileResult_ShouldPass()
5051
{
51-
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain");
52+
ActionResult result = TestDataGenerator.CreateFileContentResult();
5253

5354
result.Should()
5455
.BeFileResult();
@@ -67,7 +68,7 @@ public void BeFileResult_GivenNotFileResult_ShouldFail()
6768
[Fact]
6869
public void BeFileContentResult_GivenFileContentResult_ShouldPass()
6970
{
70-
ActionResult result = new FileContentResult(Array.Empty<byte>(), "text/plain");
71+
ActionResult result = TestDataGenerator.CreateFileContentResult();
7172

7273
result.Should()
7374
.BeFileContentResult();
@@ -83,6 +84,25 @@ public void BeFileContentResult_GivenNotFileContentResult_ShouldFail()
8384
.WithMessage("Expected ActionResult to be \"FileContentResult\", but found \"ViewResult\"");
8485
}
8586

87+
[Fact]
88+
public void BeFileStreamResult_GivenFileStreamResult_ShouldPass()
89+
{
90+
ActionResult result = TestDataGenerator.CreateFileStreamResult();
91+
92+
result.Should()
93+
.BeFileStreamResult();
94+
}
95+
96+
[Fact]
97+
public void BeFileStreamResult_GivenNotFileStreamResult_ShouldFail()
98+
{
99+
ActionResult result = new ViewResult();
100+
Action a = () => result.Should().BeFileStreamResult();
101+
102+
a.Should().Throw<Exception>()
103+
.WithMessage("Expected ActionResult to be \"FileStreamResult\", but found \"ViewResult\"");
104+
}
105+
86106
[Fact]
87107
public void BeJson_GivenJson_ShouldPass()
88108
{

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using FluentAssertions.AspNetCore.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Mvc;
23
using System;
34
using System.Text;
45
using Xunit;
@@ -8,11 +9,11 @@ namespace FluentAssertions.AspNetCore.Mvc.Tests
89
public class FileContentResultAssertions_Tests
910
{
1011
[Fact]
11-
public void WithContentType_GivenExpectedValue_ShouldPass()
12+
public void WithFileContents_GivenExpectedValue_ShouldPass()
1213
{
13-
var actualBytes = Encoding.ASCII.GetBytes("Test 1");
14-
var expectedBytes = Encoding.ASCII.GetBytes("Test 1");
15-
ActionResult result = new FileContentResult(actualBytes, "text/plain");
14+
var actualBytes = TestDataGenerator.CreateBytes("Test 1");
15+
var expectedBytes = TestDataGenerator.CreateBytes("Test 1");
16+
ActionResult result = TestDataGenerator.CreateFileContentResult(actualBytes);
1617

1718
result.Should()
1819
.BeFileContentResult()
@@ -26,12 +27,12 @@ public void WithContentType_GivenExpectedValue_ShouldPass()
2627
[InlineData(
2728
"Test 1a", "Test 2a"
2829
, "Expected \"FileContentResult.FileContents[5]\" to be 0x32, but found 0x31.")]
29-
public void WithContentType_GivenUnexpectedValue_ShouldFail(
30+
public void WithFileContents_GivenUnexpectedValue_ShouldFail(
3031
string actual, string expected, string failureMessage)
3132
{
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");
33+
var actualBytes = TestDataGenerator.CreateBytes(actual);
34+
var expectedBytes = TestDataGenerator.CreateBytes(expected);
35+
ActionResult result = TestDataGenerator.CreateFileContentResult(actualBytes);
3536

3637
Action a = () => result.Should()
3738
.BeFileContentResult()
@@ -41,5 +42,14 @@ public void WithContentType_GivenUnexpectedValue_ShouldFail(
4142
.WithMessage(failureMessage);
4243
}
4344

45+
[Fact]
46+
public void FileContents_GivenFileContentResult_ShouldHaveTheSameFileContents()
47+
{
48+
var result = TestDataGenerator.CreateFileContentResult();
49+
50+
result.Should()
51+
.BeFileContentResult()
52+
.FileContents.Should().BeSameAs(result.FileContents);
53+
}
4454
}
4555
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using FluentAssertions.AspNetCore.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using System.IO;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace FluentAssertions.AspNetCore.Mvc.Tests
9+
{
10+
public class FileStreamResultAssertions_Tests
11+
{
12+
[Fact]
13+
public void FileStream_GivenFileStreamResult_ShouldHaveTheSameStream()
14+
{
15+
var result = TestDataGenerator.CreateFileStreamResult();
16+
17+
result.Should()
18+
.BeFileStreamResult()
19+
.FileStream
20+
.Should().BeSameAs(result.FileStream);
21+
}
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.IO;
3+
using System.Text;
4+
5+
namespace FluentAssertions.AspNetCore.Mvc.Tests.Helpers
6+
{
7+
public static class TestDataGenerator
8+
{
9+
public static FileContentResult CreateFileContentResult(string content = "")
10+
{
11+
return CreateFileContentResult(CreateBytes(content));
12+
}
13+
14+
public static FileContentResult CreateFileContentResult(byte[] fileContents)
15+
{
16+
return new FileContentResult(fileContents, "text/plain");
17+
}
18+
19+
public static FileStreamResult CreateFileStreamResult(string content = "")
20+
{
21+
return CreateFileStreamResult(CreateStream(content));
22+
}
23+
24+
public static FileStreamResult CreateFileStreamResult(Stream stream)
25+
{
26+
return new FileStreamResult(stream, "text/plain");
27+
}
28+
29+
public static Stream CreateStream(string content = "")
30+
{
31+
var memoryStream = new MemoryStream();
32+
var bytes = CreateBytes(content);
33+
memoryStream.Write(bytes, 0, bytes.Length);
34+
memoryStream.Seek(0, SeekOrigin.Begin);
35+
return memoryStream;
36+
}
37+
38+
public static byte[] CreateBytes(string content = "")
39+
{
40+
var bytes = Encoding.UTF8.GetBytes(content);
41+
return bytes;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)