Skip to content

Commit 39ecef2

Browse files
committed
Merge pull request #21 from ByteBlast/master
Added support for checking for *any* file result.
2 parents a77895c + 44dd387 commit 39ecef2

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ControllerResultTestShould
2727
ReturnType<FileContentResult>(t => t.ShouldRenderFile()),
2828
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
2929
ReturnType<FilePathResult>(t=> t.ShouldRenderFilePath()),
30+
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
3031
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
3132
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
3233
};
@@ -300,6 +301,21 @@ public void Check_for_invalid_partial_name()
300301
);
301302
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName)));
302303
}
304+
#endregion
305+
306+
#region File tests
307+
308+
[Test]
309+
public void Check_for_any_file_result()
310+
{
311+
_controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile();
312+
}
313+
314+
[Test]
315+
public void Check_for_any_file_result_and_check_content_type()
316+
{
317+
_controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType);
318+
}
303319

304320
[Test]
305321
public void Check_for_file_result()
@@ -342,7 +358,6 @@ public void Check_for_file_path_result_and_check_file_name_and_check_content_typ
342358
{
343359
_controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName, ControllerResultTestController.FileContentType);
344360
}
345-
346361
#endregion
347362

348363
#region HTTP Status tests

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,41 +131,43 @@ public ActionResult DefaultPartialExplicit()
131131
return PartialView("DefaultPartialExplicit");
132132
}
133133

134-
public ActionResult EmptyFile()
134+
public ActionResult NamedView()
135135
{
136-
var content = new byte[] {};
137-
return File(content, FileContentType);
136+
return View(ViewName);
138137
}
139138

140-
public ActionResult EmptyStream()
139+
public ActionResult NamedPartial()
141140
{
142-
var content = new MemoryStream();
143-
return File(content, FileContentType);
141+
return PartialView(PartialName);
144142
}
145143

146-
public ActionResult EmptyFilePath()
144+
public ActionResult RandomView()
147145
{
148-
return File(FileName, FileContentType);
146+
return View(RandomViewName);
149147
}
150148

151-
public ActionResult NamedView()
149+
public ActionResult RandomPartial()
152150
{
153-
return View(ViewName);
151+
return PartialView(RandomViewName);
154152
}
153+
#endregion
155154

156-
public ActionResult NamedPartial()
155+
#region Files
156+
public ActionResult EmptyFile()
157157
{
158-
return PartialView(PartialName);
158+
var content = new byte[] { };
159+
return File(content, FileContentType);
159160
}
160161

161-
public ActionResult RandomView()
162+
public ActionResult EmptyStream()
162163
{
163-
return View(RandomViewName);
164+
var content = new MemoryStream();
165+
return File(content, FileContentType);
164166
}
165167

166-
public ActionResult RandomPartial()
168+
public ActionResult EmptyFilePath()
167169
{
168-
return PartialView(RandomViewName);
170+
return File(FileName, FileContentType);
169171
}
170172
#endregion
171173

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,24 @@ public ViewResultTest ShouldRenderDefaultPartialView()
210210
return ShouldRenderPartialView(_actionName);
211211
}
212212

213+
#endregion
214+
215+
#region File Results
216+
217+
public FileResult ShouldRenderAnyFile(string contentType = null)
218+
{
219+
ValidateActionReturnType<FileResult>();
220+
221+
var fileResult = (FileResult)_actionResult;
222+
223+
if (contentType != null && fileResult.ContentType != contentType)
224+
{
225+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
226+
}
227+
228+
return fileResult;
229+
}
230+
213231
public FileContentResult ShouldRenderFile(string contentType = null)
214232
{
215233
ValidateActionReturnType<FileContentResult>();
@@ -242,7 +260,7 @@ public FilePathResult ShouldRenderFilePath(string fileName = null, string conten
242260
{
243261
ValidateActionReturnType<FilePathResult>();
244262

245-
var fileResult = (FilePathResult) _actionResult;
263+
var fileResult = (FilePathResult)_actionResult;
246264

247265
if (fileName != null && fileName != fileResult.FileName)
248266
{

0 commit comments

Comments
 (0)