Skip to content

Commit c25d2e7

Browse files
committed
Refactored certain file methods to test the content type first.
Partial implementation of #3
1 parent 2dd41eb commit c25d2e7

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ public void Check_for_file_content_result_and_check_invalid_content_type()
382382
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
383383
}
384384

385+
[Test]
386+
public void Check_for_file_content_result_and_check_invalid_binary_content_and_check_invalid_content_type()
387+
{
388+
byte[] contents = { 1, 2 };
389+
const string contentType = "application/dummy";
390+
391+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
392+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents, contentType));
393+
394+
// When supplied with both an invalid content type and invalid content, test the content type first.
395+
Assert.That(exception.Message.Contains("content type"));
396+
}
397+
385398
[Test]
386399
public void Check_for_file_result()
387400
{
@@ -457,6 +470,19 @@ public void Check_for_file_path_result_and_check_file_name_and_check_invalid_con
457470
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
458471
}
459472

473+
[Test]
474+
public void Check_for_file_path_result_and_check_invalid_file_name_and_check_invalid_content_type()
475+
{
476+
const string contentType = "application/dummy";
477+
const string name = "dummy";
478+
479+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
480+
_controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType));
481+
482+
// When supplied with both an invalid content type and invalid file name, test the content type first.
483+
Assert.That(exception.Message.Contains("content type"));
484+
}
485+
460486
#endregion
461487

462488
#region HTTP Status tests

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
237237

238238
var fileResult = (FileContentResult) _actionResult;
239239

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+
}
244+
240245
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
241246
{
242247
throw new ActionResultAssertionException(string.Format(
@@ -245,11 +250,6 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
245250
string.Join(", ", fileResult.FileContents)));
246251
}
247252

248-
if (contentType != null && fileResult.ContentType != contentType)
249-
{
250-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
251-
}
252-
253253
return fileResult;
254254
}
255255

@@ -288,14 +288,14 @@ public FilePathResult ShouldRenderFilePath(string fileName = null, string conten
288288

289289
var fileResult = (FilePathResult)_actionResult;
290290

291-
if (fileName != null && fileName != fileResult.FileName)
291+
if (contentType != null && fileResult.ContentType != contentType)
292292
{
293-
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
293+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
294294
}
295295

296-
if (contentType != null && fileResult.ContentType != contentType)
296+
if (fileName != null && fileName != fileResult.FileName)
297297
{
298-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
298+
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
299299
}
300300

301301
return fileResult;

0 commit comments

Comments
 (0)