Skip to content

Commit 253b065

Browse files
committed
Refactored file assertion methods.
1 parent e94f489 commit 253b065

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public void Check_for_file_stream_result_and_check_invalid_stream_content()
502502

503503
var expected = string.Format("[{0}]", string.Join(", ", buffer));
504504
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyFileBuffer));
505-
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
505+
var message = string.Format("Expected file contents to be equal to {0}, but instead was given {1}.", expected, actual);
506506

507507
Assert.That(exception.Message, Is.EqualTo(message));
508508
}
@@ -518,7 +518,7 @@ public void Check_for_file_stream_result_with_populated_file_and_check_invalid_s
518518

519519
var expected = string.Format("[{0}]", string.Join(", ", buffer));
520520
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents));
521-
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
521+
var message = string.Format("Expected file contents to be equal to {0}, but instead was given {1}.", expected, actual);
522522

523523
Assert.That(exception.Message, Is.EqualTo(message));
524524
}
@@ -539,7 +539,7 @@ public void Check_for_file_stream_result_and_check_invalid_content_type()
539539
_controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType));
540540

541541
Assert.That(exception.Message, Is.EqualTo(string.Format(
542-
"Expected stream to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
542+
"Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
543543
}
544544

545545
[Test]
@@ -573,7 +573,7 @@ public void Check_for_file_stream_result_and_check_invalid_binary_content()
573573

574574
var expected = string.Format("[{0}]", string.Join(", ", content));
575575
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents));
576-
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
576+
var message = string.Format("Expected file contents to be equal to {0}, but instead was given {1}.", expected, actual);
577577

578578
Assert.That(exception.Message, Is.EqualTo(message));
579579
}
@@ -593,7 +593,7 @@ public void Check_for_file_stream_result_and_check_binary_content_and_check_inva
593593
var exception = Assert.Throws<ActionResultAssertionException>(() =>
594594
_controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents, contentType));
595595

596-
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.",
596+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.",
597597
contentType, ControllerResultTestController.FileContentType)));
598598
}
599599

@@ -643,7 +643,7 @@ public void Check_for_file_stream_result_and_check_textual_content_and_check_inv
643643
var exception = Assert.Throws<ActionResultAssertionException>(() =>
644644
_controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, contentType));
645645

646-
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.",
646+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.",
647647
contentType, ControllerResultTestController.FileContentType)));
648648
}
649649

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView()
217217

218218
#region File Results
219219

220-
public FileResult ShouldRenderAnyFile(string contentType = null)
220+
private static void EnsureContentTypeIsSame(string actual, string expected)
221221
{
222-
ValidateActionReturnType<FileResult>();
223-
224-
var fileResult = (FileResult)_actionResult;
222+
if (expected == null) return;
223+
if (actual != expected)
224+
{
225+
throw new ActionResultAssertionException(string.Format(
226+
"Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual));
227+
}
228+
}
225229

226-
if (contentType != null && fileResult.ContentType != contentType)
230+
private static byte[] ConvertStreamToArray(Stream stream)
231+
{
232+
using (var memoryStream = new MemoryStream())
227233
{
228-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
234+
stream.CopyTo(memoryStream);
235+
stream.Position = 0;
236+
return memoryStream.ToArray();
229237
}
238+
}
239+
240+
public FileResult ShouldRenderAnyFile(string contentType = null)
241+
{
242+
ValidateActionReturnType<FileResult>();
243+
var fileResult = (FileResult)_actionResult;
230244

245+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
246+
231247
return fileResult;
232248
}
233249

234250
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
235251
{
236252
ValidateActionReturnType<FileContentResult>();
237-
238253
var fileResult = (FileContentResult) _actionResult;
239254

240-
if (contentType != null && fileResult.ContentType != contentType)
241-
{
242-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
243-
}
255+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
244256

245257
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
246258
{
@@ -256,45 +268,37 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
256268
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
257269
{
258270
ValidateActionReturnType<FileContentResult>();
259-
260271
var fileResult = (FileContentResult)_actionResult;
261272

262-
if (contentType != null && fileResult.ContentType != contentType)
263-
{
264-
throw new ActionResultAssertionException(
265-
string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType,
266-
fileResult.ContentType));
267-
}
273+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
268274

269275
if (encoding == null)
270276
encoding = Encoding.UTF8;
271277

272278
var reconstitutedText = encoding.GetString(fileResult.FileContents);
273279
if (contents != reconstitutedText)
274280
{
275-
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
281+
throw new ActionResultAssertionException(string.Format(
282+
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
283+
contents,
284+
reconstitutedText));
276285
}
277286

278287
return fileResult;
279288
}
280289

281290
public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
282291
{
283-
return ShouldRenderFileStream(new MemoryStream(content), contentType);
292+
var reconstitutedStream = new MemoryStream(content);
293+
return ShouldRenderFileStream(reconstitutedStream, contentType);
284294
}
285295

286296
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
287297
{
288298
ValidateActionReturnType<FileStreamResult>();
289299
var fileResult = (FileStreamResult)_actionResult;
290300

291-
if (contentType != null && fileResult.ContentType != contentType)
292-
{
293-
throw new ActionResultAssertionException(string.Format(
294-
"Expected stream to be of content type '{0}', but instead was given '{1}'.",
295-
contentType,
296-
fileResult.ContentType));
297-
}
301+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
298302

299303
if (stream != null)
300304
{
@@ -304,7 +308,7 @@ public FileStreamResult ShouldRenderFileStream(Stream stream = null, string cont
304308
if (!expected.SequenceEqual(actual))
305309
{
306310
throw new ActionResultAssertionException(string.Format(
307-
"Expected stream contents to be equal to [{0}], but instead was given [{1}].",
311+
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
308312
string.Join(", ", expected),
309313
string.Join(", ", actual)));
310314
}
@@ -318,13 +322,7 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
318322
ValidateActionReturnType<FileStreamResult>();
319323
var fileResult = (FileStreamResult)_actionResult;
320324

321-
if (contentType != null && fileResult.ContentType != contentType)
322-
{
323-
throw new ActionResultAssertionException(string.Format(
324-
"Expected stream to be of content type '{0}', but instead was given '{1}'.",
325-
contentType,
326-
fileResult.ContentType));
327-
}
325+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
328326

329327
if (encoding == null)
330328
encoding = Encoding.UTF8;
@@ -341,30 +339,19 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
341339
return fileResult;
342340
}
343341

344-
private static byte[] ConvertStreamToArray(Stream stream)
345-
{
346-
using (var memoryStream = new MemoryStream())
347-
{
348-
stream.CopyTo(memoryStream);
349-
stream.Position = 0;
350-
return memoryStream.ToArray();
351-
}
352-
}
353-
354342
public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null)
355343
{
356344
ValidateActionReturnType<FilePathResult>();
357-
358345
var fileResult = (FilePathResult)_actionResult;
359346

360-
if (contentType != null && fileResult.ContentType != contentType)
361-
{
362-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
363-
}
347+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
364348

365349
if (fileName != null && fileName != fileResult.FileName)
366350
{
367-
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
351+
throw new ActionResultAssertionException(string.Format(
352+
"Expected file name to be '{0}', but instead was given '{1}'.",
353+
fileName,
354+
fileResult.FileName));
368355
}
369356

370357
return fileResult;

0 commit comments

Comments
 (0)