Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored file assertion methods.
  • Loading branch information
AlexArchive committed Sep 2, 2014
commit 253b065e0962622d1088faab97abb95b9d5ddb98
12 changes: 6 additions & 6 deletions TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public void Check_for_file_stream_result_and_check_invalid_stream_content()

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

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

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

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

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

[Test]
Expand Down Expand Up @@ -573,7 +573,7 @@ public void Check_for_file_stream_result_and_check_invalid_binary_content()

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

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

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

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

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

Expand Down
89 changes: 38 additions & 51 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView()

#region File Results

public FileResult ShouldRenderAnyFile(string contentType = null)
private static void EnsureContentTypeIsSame(string actual, string expected)
{
ValidateActionReturnType<FileResult>();

var fileResult = (FileResult)_actionResult;
if (expected == null) return;
if (actual != expected)
{
throw new ActionResultAssertionException(string.Format(
"Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual));
}
}

if (contentType != null && fileResult.ContentType != contentType)
private static byte[] ConvertStreamToArray(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
stream.CopyTo(memoryStream);
stream.Position = 0;
return memoryStream.ToArray();
}
}

public FileResult ShouldRenderAnyFile(string contentType = null)
{
ValidateActionReturnType<FileResult>();
var fileResult = (FileResult)_actionResult;

EnsureContentTypeIsSame(fileResult.ContentType, contentType);

return fileResult;
}

public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
{
ValidateActionReturnType<FileContentResult>();

var fileResult = (FileContentResult) _actionResult;

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

if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
{
Expand All @@ -256,45 +268,37 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
{
ValidateActionReturnType<FileContentResult>();

var fileResult = (FileContentResult)_actionResult;

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

if (encoding == null)
encoding = Encoding.UTF8;

var reconstitutedText = encoding.GetString(fileResult.FileContents);
if (contents != reconstitutedText)
{
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
contents,
reconstitutedText));
}

return fileResult;
}

public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
{
return ShouldRenderFileStream(new MemoryStream(content), contentType);
var reconstitutedStream = new MemoryStream(content);
return ShouldRenderFileStream(reconstitutedStream, contentType);
}

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

if (contentType != null && fileResult.ContentType != contentType)
{
throw new ActionResultAssertionException(string.Format(
"Expected stream to be of content type '{0}', but instead was given '{1}'.",
contentType,
fileResult.ContentType));
}
EnsureContentTypeIsSame(fileResult.ContentType, contentType);

if (stream != null)
{
Expand All @@ -304,7 +308,7 @@ public FileStreamResult ShouldRenderFileStream(Stream stream = null, string cont
if (!expected.SequenceEqual(actual))
{
throw new ActionResultAssertionException(string.Format(
"Expected stream contents to be equal to [{0}], but instead was given [{1}].",
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
string.Join(", ", expected),
string.Join(", ", actual)));
}
Expand All @@ -318,13 +322,7 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
ValidateActionReturnType<FileStreamResult>();
var fileResult = (FileStreamResult)_actionResult;

if (contentType != null && fileResult.ContentType != contentType)
{
throw new ActionResultAssertionException(string.Format(
"Expected stream to be of content type '{0}', but instead was given '{1}'.",
contentType,
fileResult.ContentType));
}
EnsureContentTypeIsSame(fileResult.ContentType, contentType);

if (encoding == null)
encoding = Encoding.UTF8;
Expand All @@ -341,30 +339,19 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
return fileResult;
}

private static byte[] ConvertStreamToArray(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
stream.Position = 0;
return memoryStream.ToArray();
}
}

public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null)
{
ValidateActionReturnType<FilePathResult>();

var fileResult = (FilePathResult)_actionResult;

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

if (fileName != null && fileName != fileResult.FileName)
{
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
throw new ActionResultAssertionException(string.Format(
"Expected file name to be '{0}', but instead was given '{1}'.",
fileName,
fileResult.FileName));
}

return fileResult;
Expand Down