Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion NextVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0
2.1.0
83 changes: 83 additions & 0 deletions TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ControllerResultTestShould
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
ReturnType<ContentResult>(t => t.ShouldReturnContent()),
ReturnType<ContentResult>(t => t.ShouldReturnContent("")),
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "")),
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "", Encoding.UTF8))
};
// Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion
private static readonly List<RedirectToActionTestMetadata> ActionRedirects = new List<RedirectToActionTestMetadata>
Expand Down Expand Up @@ -770,5 +774,84 @@ public void Allow_the_object_that_is_returned_to_be_checked()
_controller.WithCallTo(c => c.Json()).ShouldReturnJson(d => Assert.That(d, Is.EqualTo(ControllerResultTestController.JsonValue)));
}
#endregion

#region Content tests

[Test]
public void Check_for_content_result()
{
_controller.WithCallTo(c => c.Content()).ShouldReturnContent();
}

[Test]
public void Check_for_content_result_and_check_content()
{
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent);
}

[Test]
public void Check_for_content_result_and_check_invalid_content()
{
const string content = "dummy contents";

var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content to be \"{0}\", but instead was \"{1}\".", content, ControllerResultTestController.TextualContent)));
}

[Test]
public void Check_for_content_result_and_check_content_and_check_content_type()
{
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType);
}

[Test]
public void Check_for_content_result_and_check_content_and_check_invalid_content_type()
{
const string contentType = "application/dummy";

var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, contentType));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content type to be \"{0}\", but instead was \"{1}\".", contentType, ControllerResultTestController.ContentType)));
}

[Test]
public void Check_for_content_result_and_check_content_and_check_content_type_and_check_content_encoding()
{
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, ControllerResultTestController.TextualContentEncoding);
}

[Test]
public void Check_for_content_result_and_check_content_and_check_content_type_and_check_invalid_content_encoding()
{
var encoding = Encoding.Unicode;

var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, encoding));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was {1}.", encoding.EncodingName, ControllerResultTestController.TextualContentEncoding.EncodingName)));
}

[Test]
public void Check_for_content_result_and_check_invalid_content_and_check_invalid_content_type_and_check_invalid_encoding()
{
const string contentType = "application/dummy";
const string content = "dumb";
Encoding encoding = Encoding.Unicode;

var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content, contentType, encoding));

// Assert that the content type validation occurs before that of the actual content.
Assert.That(exception.Message.Contains("content type"));
}

[Test]
public void Emit_readable_error_message_when_the_actual_content_encoding_has_not_been_specified()
{
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.ContentWithoutEncodingSpecified()).ShouldReturnContent(encoding: ControllerResultTestController.TextualContentEncoding));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was null.", ControllerResultTestController.TextualContentEncoding.EncodingName)));
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class ControllerResultTestController : Controller
public static byte[] EmptyFileBuffer = { };
public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyFileBuffer);
public static readonly Stream BinaryStreamContents = new MemoryStream(BinaryFileContents);

public const string TextualContent = "textual content";
public static readonly Encoding TextualContentEncoding = Encoding.UTF8;
public const string ContentType = "application/contentType";
#endregion

#region Empty, Null and Random Results
Expand Down Expand Up @@ -227,6 +229,20 @@ public ActionResult Json()
return Json(JsonValue);
}
#endregion

#region Content

public ActionResult Content()
{
return Content(TextualContent, ContentType, TextualContentEncoding);
}

public ActionResult ContentWithoutEncodingSpecified()
{
return Content(TextualContent, ContentType);
}

#endregion
}

#region Test Classes
Expand Down
36 changes: 36 additions & 0 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,41 @@ public void ShouldReturnJson(Action<dynamic> assertion)
assertion(jsonResult.Data);
}
#endregion

#region Content

public ContentResult ShouldReturnContent(string content = null, string contentType = null, Encoding encoding = null)
{
ValidateActionReturnType<ContentResult>();
var contentResult = (ContentResult) _actionResult;

if (contentType != null && contentType != contentResult.ContentType)
{
throw new ActionResultAssertionException(string.Format(
"Expected content type to be \"{0}\", but instead was \"{1}\".",
contentType,
contentResult.ContentType));
}

if (content != null && content != contentResult.Content)
{
throw new ActionResultAssertionException(string.Format(
"Expected content to be \"{0}\", but instead was \"{1}\".",
content,
contentResult.Content));
}

if (encoding != null && encoding != contentResult.ContentEncoding)
{
throw new ActionResultAssertionException(string.Format(
"Expected encoding to be equal to {0}, but instead was {1}.",
encoding.EncodingName,
contentResult.ContentEncoding != null ? contentResult.ContentEncoding.EncodingName : "null"));
}

return contentResult;
}

#endregion
}
}