Skip to content

Commit dbd746a

Browse files
committed
Added support for checking a file content result's binary contents.
Partial implementation of #3
1 parent 907b1eb commit dbd746a

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Net;
56
using System.Web.Mvc;
@@ -28,6 +29,7 @@ class ControllerResultTestShould
2829
ReturnType<FileContentResult>(t => t.ShouldRenderFile("")),
2930
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
3031
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
32+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
3133
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3234
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3335
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -343,6 +345,25 @@ public void Check_for_file_content_result()
343345
_controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents();
344346
}
345347

348+
[Test]
349+
public void Check_for_file_content_result_and_check_binary_content()
350+
{
351+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
352+
}
353+
354+
[Test]
355+
public void Check_for_file_content_result_and_check_invalid_binary_content()
356+
{
357+
byte[] contents = { 1, 2 };
358+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
359+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents));
360+
361+
Assert.True(exception.Message.StartsWith("Expected file contents to be "));
362+
Assert.True(exception.Message.EndsWith("."));
363+
Assert.True(string.Join(",", contents).All(exception.Message.Contains));
364+
Assert.True(string.Join(",", ControllerResultTestController.FileContents).All(exception.Message.Contains));
365+
}
366+
346367
[Test]
347368
public void Check_for_file_result()
348369
{

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ControllerResultTestController : Controller
1515
public const int Code = 403;
1616
public const string JsonValue = "json";
1717
public const string FileName = "NamedFile";
18+
public static byte[] FileContents = { 1 };
1819
#endregion
1920

2021
#region Empty, Null and Random Results
@@ -161,7 +162,7 @@ public ActionResult EmptyFile()
161162

162163
public ActionResult File()
163164
{
164-
return EmptyFile();
165+
return File(FileContents, FileContentType);
165166
}
166167

167168
public ActionResult EmptyStream()

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Linq.Expressions;
34
using System.Net;
45
using System.Reflection;
@@ -229,10 +230,21 @@ public FileResult ShouldRenderAnyFile(string contentType = null)
229230
return fileResult;
230231
}
231232

232-
public FileContentResult ShouldRenderFileContents()
233+
public FileContentResult ShouldRenderFileContents(byte[] contents = null)
233234
{
234235
ValidateActionReturnType<FileContentResult>();
235-
return (FileContentResult) _actionResult;
236+
237+
var fileResult = (FileContentResult) _actionResult;
238+
239+
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
240+
{
241+
throw new ActionResultAssertionException(string.Format(
242+
"Expected file contents to be equal to {0}, but instead was given {1}.",
243+
string.Join(",", contents),
244+
string.Join(",", fileResult.FileContents)));
245+
}
246+
247+
return fileResult;
236248
}
237249

238250
public FileContentResult ShouldRenderFile(string contentType = null)

0 commit comments

Comments
 (0)