Skip to content

Commit 6d52160

Browse files
committed
Merge pull request #32 from ByteBlast/master
Added support for checking content.
2 parents aa0a4c8 + cf605f9 commit 6d52160

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

NextVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.0
1+
2.1.0

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class ControllerResultTestShould
4949
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
5050
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
5151
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
52+
ReturnType<ContentResult>(t => t.ShouldReturnContent()),
53+
ReturnType<ContentResult>(t => t.ShouldReturnContent("")),
54+
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "")),
55+
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "", Encoding.UTF8))
5256
};
5357
// Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion
5458
private static readonly List<RedirectToActionTestMetadata> ActionRedirects = new List<RedirectToActionTestMetadata>
@@ -770,5 +774,84 @@ public void Allow_the_object_that_is_returned_to_be_checked()
770774
_controller.WithCallTo(c => c.Json()).ShouldReturnJson(d => Assert.That(d, Is.EqualTo(ControllerResultTestController.JsonValue)));
771775
}
772776
#endregion
777+
778+
#region Content tests
779+
780+
[Test]
781+
public void Check_for_content_result()
782+
{
783+
_controller.WithCallTo(c => c.Content()).ShouldReturnContent();
784+
}
785+
786+
[Test]
787+
public void Check_for_content_result_and_check_content()
788+
{
789+
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent);
790+
}
791+
792+
[Test]
793+
public void Check_for_content_result_and_check_invalid_content()
794+
{
795+
const string content = "dummy contents";
796+
797+
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content));
798+
799+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content to be \"{0}\", but instead was \"{1}\".", content, ControllerResultTestController.TextualContent)));
800+
}
801+
802+
[Test]
803+
public void Check_for_content_result_and_check_content_and_check_content_type()
804+
{
805+
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType);
806+
}
807+
808+
[Test]
809+
public void Check_for_content_result_and_check_content_and_check_invalid_content_type()
810+
{
811+
const string contentType = "application/dummy";
812+
813+
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, contentType));
814+
815+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content type to be \"{0}\", but instead was \"{1}\".", contentType, ControllerResultTestController.ContentType)));
816+
}
817+
818+
[Test]
819+
public void Check_for_content_result_and_check_content_and_check_content_type_and_check_content_encoding()
820+
{
821+
_controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, ControllerResultTestController.TextualContentEncoding);
822+
}
823+
824+
[Test]
825+
public void Check_for_content_result_and_check_content_and_check_content_type_and_check_invalid_content_encoding()
826+
{
827+
var encoding = Encoding.Unicode;
828+
829+
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, encoding));
830+
831+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was {1}.", encoding.EncodingName, ControllerResultTestController.TextualContentEncoding.EncodingName)));
832+
}
833+
834+
[Test]
835+
public void Check_for_content_result_and_check_invalid_content_and_check_invalid_content_type_and_check_invalid_encoding()
836+
{
837+
const string contentType = "application/dummy";
838+
const string content = "dumb";
839+
Encoding encoding = Encoding.Unicode;
840+
841+
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content, contentType, encoding));
842+
843+
// Assert that the content type validation occurs before that of the actual content.
844+
Assert.That(exception.Message.Contains("content type"));
845+
}
846+
847+
[Test]
848+
public void Emit_readable_error_message_when_the_actual_content_encoding_has_not_been_specified()
849+
{
850+
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.ContentWithoutEncodingSpecified()).ShouldReturnContent(encoding: ControllerResultTestController.TextualContentEncoding));
851+
852+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was null.", ControllerResultTestController.TextualContentEncoding.EncodingName)));
853+
}
854+
855+
#endregion
773856
}
774857
}

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class ControllerResultTestController : Controller
2121
public static byte[] EmptyFileBuffer = { };
2222
public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyFileBuffer);
2323
public static readonly Stream BinaryStreamContents = new MemoryStream(BinaryFileContents);
24-
24+
public const string TextualContent = "textual content";
25+
public static readonly Encoding TextualContentEncoding = Encoding.UTF8;
26+
public const string ContentType = "application/contentType";
2527
#endregion
2628

2729
#region Empty, Null and Random Results
@@ -227,6 +229,20 @@ public ActionResult Json()
227229
return Json(JsonValue);
228230
}
229231
#endregion
232+
233+
#region Content
234+
235+
public ActionResult Content()
236+
{
237+
return Content(TextualContent, ContentType, TextualContentEncoding);
238+
}
239+
240+
public ActionResult ContentWithoutEncodingSpecified()
241+
{
242+
return Content(TextualContent, ContentType);
243+
}
244+
245+
#endregion
230246
}
231247

232248
#region Test Classes

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,41 @@ public void ShouldReturnJson(Action<dynamic> assertion)
396396
assertion(jsonResult.Data);
397397
}
398398
#endregion
399+
400+
#region Content
401+
402+
public ContentResult ShouldReturnContent(string content = null, string contentType = null, Encoding encoding = null)
403+
{
404+
ValidateActionReturnType<ContentResult>();
405+
var contentResult = (ContentResult) _actionResult;
406+
407+
if (contentType != null && contentType != contentResult.ContentType)
408+
{
409+
throw new ActionResultAssertionException(string.Format(
410+
"Expected content type to be \"{0}\", but instead was \"{1}\".",
411+
contentType,
412+
contentResult.ContentType));
413+
}
414+
415+
if (content != null && content != contentResult.Content)
416+
{
417+
throw new ActionResultAssertionException(string.Format(
418+
"Expected content to be \"{0}\", but instead was \"{1}\".",
419+
content,
420+
contentResult.Content));
421+
}
422+
423+
if (encoding != null && encoding != contentResult.ContentEncoding)
424+
{
425+
throw new ActionResultAssertionException(string.Format(
426+
"Expected encoding to be equal to {0}, but instead was {1}.",
427+
encoding.EncodingName,
428+
contentResult.ContentEncoding != null ? contentResult.ContentEncoding.EncodingName : "null"));
429+
}
430+
431+
return contentResult;
432+
}
433+
434+
#endregion
399435
}
400436
}

0 commit comments

Comments
 (0)