From 4f29c7279d9b33b519682922bd5c7a49b5ce7eca Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Sun, 7 Sep 2014 23:22:42 +0100 Subject: [PATCH 1/5] Added support for checking content (ContentResult). Implementation of #12. --- .../ControllerResultTestTests.cs | 75 +++++++++++++++++++ .../ControllerResultTestController.cs | 13 +++- .../ControllerResultTest.cs | 36 +++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 63acbcc..8b21c70 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -49,6 +49,10 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), + ReturnType(t => t.ShouldReturnContent()), + ReturnType(t => t.ShouldReturnContent("")), + ReturnType(t => t.ShouldReturnContent("", "")), + ReturnType(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 ActionRedirects = new List @@ -770,5 +774,76 @@ 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(() => _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(() => _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(() => _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(() => _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")); + } + + #endregion } } diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 2f1e9e4..e203dbb 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -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 @@ -227,6 +229,15 @@ public ActionResult Json() return Json(JsonValue); } #endregion + + #region Content + + public ActionResult Content() + { + return Content(TextualContent, ContentType, TextualContentEncoding); + } + + #endregion } #region Test Classes diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 0570b06..180fdd8 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -396,5 +396,41 @@ public void ShouldReturnJson(Action assertion) assertion(jsonResult.Data); } #endregion + + #region Content + + public ContentResult ShouldReturnContent(string content = null, string contentType = null, Encoding encoding = null) + { + ValidateActionReturnType(); + 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.EncodingName)); + } + + return contentResult; + } + + #endregion } } \ No newline at end of file From 1ebe99c5c1158b2ab1022e0c7e35e01a0121748c Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Sun, 7 Sep 2014 23:31:01 +0100 Subject: [PATCH 2/5] Surrounded expected and actual values in quotes. --- .../ControllerResultTestTests.cs | 4 ++-- TestStack.FluentMvcTesting/ControllerResultTest.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 8b21c70..218301a 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -796,7 +796,7 @@ public void Check_for_content_result_and_check_invalid_content() var exception = Assert.Throws(() => _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))); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content to be \"{0}\", but instead was \"{1}\".", content, ControllerResultTestController.TextualContent))); } [Test] @@ -812,7 +812,7 @@ public void Check_for_content_result_and_check_content_and_check_invalid_content var exception = Assert.Throws(() => _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))); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content type to be \"{0}\", but instead was \"{1}\".", contentType, ControllerResultTestController.ContentType))); } [Test] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 180fdd8..19a2297 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -407,7 +407,7 @@ public ContentResult ShouldReturnContent(string content = null, string contentTy if (contentType != null && contentType != contentResult.ContentType) { throw new ActionResultAssertionException(string.Format( - "Expected content type to be {0}, but instead was {1}.", + "Expected content type to be \"{0}\", but instead was \"{1}\".", contentType, contentResult.ContentType)); } @@ -415,12 +415,12 @@ public ContentResult ShouldReturnContent(string content = null, string contentTy if (content != null && content != contentResult.Content) { throw new ActionResultAssertionException(string.Format( - "Expected content to be {0}, but instead was {1}.", + "Expected content to be \"{0}\", but instead was \"{1}\".", content, contentResult.Content)); } - if (encoding != null && encoding != contentResult.ContentEncoding) + if (encoding != null && encoding != contentResult.ContentEncoding) { throw new ActionResultAssertionException(string.Format( "Expected encoding to be equal to {0}, but instead was {1}.", From a9166400ff348925486d9a55a7d4445ca3c779ef Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Sun, 7 Sep 2014 23:40:58 +0100 Subject: [PATCH 3/5] Tweak to prevent confusing NRE from surfacing. --- .../ControllerResultTestTests.cs | 8 ++++++++ .../TestControllers/ControllerResultTestController.cs | 5 +++++ TestStack.FluentMvcTesting/ControllerResultTest.cs | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 218301a..217e0e7 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -844,6 +844,14 @@ public void Check_for_content_result_and_check_invalid_content_and_check_invalid 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(() => _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 } } diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index e203dbb..8d7aa0f 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -237,6 +237,11 @@ public ActionResult Content() return Content(TextualContent, ContentType, TextualContentEncoding); } + public ActionResult ContentWithoutEncodingSpecified() + { + return Content(TextualContent, ContentType); + } + #endregion } diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 19a2297..7dcd3d1 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -425,7 +425,7 @@ public ContentResult ShouldReturnContent(string content = null, string contentTy throw new ActionResultAssertionException(string.Format( "Expected encoding to be equal to {0}, but instead was {1}.", encoding.EncodingName, - contentResult.ContentEncoding.EncodingName)); + contentResult.ContentEncoding != null ? contentResult.ContentEncoding.EncodingName : @"null")); } return contentResult; From a85d7a2e1d745dce49004e8049a9747f7326b6cf Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 8 Sep 2014 00:02:46 +0100 Subject: [PATCH 4/5] Bumped minor version. --- NextVersion.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NextVersion.txt b/NextVersion.txt index 359a5b9..50aea0e 100644 --- a/NextVersion.txt +++ b/NextVersion.txt @@ -1 +1 @@ -2.0.0 \ No newline at end of file +2.1.0 \ No newline at end of file From cf605f91d253a6cf7a1e20e038e6195769a4a6c7 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 8 Sep 2014 15:33:50 +0100 Subject: [PATCH 5/5] Replaced verbatim string literal with a regular one. --- TestStack.FluentMvcTesting/ControllerResultTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 7dcd3d1..38a778a 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -425,7 +425,7 @@ public ContentResult ShouldReturnContent(string content = null, string contentTy 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")); + contentResult.ContentEncoding != null ? contentResult.ContentEncoding.EncodingName : "null")); } return contentResult;