diff --git a/.gitignore b/.gitignore index 99bdabd..c35faf5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,4 @@ _ReSharper* */NUnit*/tools */NhibernateProfiler*/tools packages -*.DotSettings /TestStack.FluentMVCTesting.Example/App_Data diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs deleted file mode 100644 index 217e0e7..0000000 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ /dev/null @@ -1,857 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Net; -using System.Web.Mvc; -using NUnit.Framework; -using TestStack.FluentMVCTesting.Tests.TestControllers; -using System.Text; - - -namespace TestStack.FluentMVCTesting.Tests -{ - [TestFixture] - class ControllerResultTestShould - { - #region Test cases - #pragma warning disable 169 - // Expected action return types for the different types of assertions - private static readonly List> ReturnTypes = new List> - { - ReturnType(t => t.ShouldReturnEmptyResult()), - ReturnType(t => t.ShouldRedirectTo("")), - ReturnType(t => t.ShouldRedirectTo(c => c.EmptyResult)), - ReturnType(t => t.ShouldRedirectTo(c => c.SomeAction())), - ReturnType(t => t.ShouldRenderView("")), - ReturnType(t => t.ShouldRenderDefaultView()), - ReturnType(t => t.ShouldRenderPartialView("")), - ReturnType(t => t.ShouldRenderDefaultPartialView()), - ReturnType(t => t.ShouldRenderFileContents()), - ReturnType(t => t.ShouldRenderFileContents(new byte[0])), - ReturnType(t => t.ShouldRenderFileContents(new byte[0], "")), - ReturnType(t => t.ShouldRenderFileContents("")), - ReturnType(t => t.ShouldRenderFileContents("", "")), - ReturnType(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)), - ReturnType(t => t.ShouldRenderFilePath()), - ReturnType(t => t.ShouldRenderFilePath("")), - ReturnType(t => t.ShouldRenderFilePath("", "")), - ReturnType(t => t.ShouldRenderFileStream()), - ReturnType(t => t.ShouldRenderFileStream(new MemoryStream())), - ReturnType(t => t.ShouldRenderFileStream(contentType: "")), - ReturnType(t => t.ShouldRenderFileStream(new MemoryStream(), "")), - ReturnType(t => t.ShouldRenderFileStream(new byte[0])), - ReturnType(t => t.ShouldRenderFileStream(new byte[0], "")), - ReturnType(t => t.ShouldRenderFileStream("")), - ReturnType(t => t.ShouldRenderFileStream("", "")), - ReturnType(t => t.ShouldRenderFileStream("", "", Encoding.UTF8)), - 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 - { - ActionRedirect("ActionWithNoParameters", - t => t.ShouldRedirectTo(c => c.ActionWithNoParameters), - c => c.RedirectToActionWithNoParameters() - ), - ActionRedirect("ActionWithOneParameter", - t => t.ShouldRedirectTo(c => c.ActionWithOneParameter), - c => c.RedirectToActionWithOneParameter() - ), - ActionRedirect("ActionWithOneParameter", - t => t.ShouldRedirectTo(c => c.ActionWithOneParameter), - c => c.RedirectToActionWithOneParameter() - ), - ActionRedirect("ActionWithTwoParameters", - t => t.ShouldRedirectTo(c => c.ActionWithTwoParameters), - c => c.RedirectToActionWithTwoParameters() - ), - ActionRedirect("ActionWithThreeParameters", - t => t.ShouldRedirectTo(c => c.ActionWithThreeParameters), - c => c.RedirectToActionWithThreeParameters() - ), - ActionRedirect("ActionWithNoParameters", - t => t.ShouldRedirectTo(c => c.ActionWithNoParameters()), - c => c.RedirectToActionWithNoParameters() - ), - ActionRedirect("ActionWithOneParameter", - t => t.ShouldRedirectTo(c => c.ActionWithOneParameter(0)), - c => c.RedirectToActionWithOneParameter() - ), - ActionRedirect("ActionWithTwoParameters", - t => t.ShouldRedirectTo(c => c.ActionWithTwoParameters(0, 0)), - c => c.RedirectToActionWithTwoParameters() - ), - ActionRedirect("ActionWithThreeParameters", - t => t.ShouldRedirectTo(c => c.ActionWithThreeParameters(0, 0, 0)), - c => c.RedirectToActionWithThreeParameters() - ), - ActionRedirect("ActionWithMoreThanThreeParameters", - t => t.ShouldRedirectTo(c => c.ActionWithMoreThanThreeParameters(0, 0, 0, 0)), - c => c.RedirectToActionWithMoreThanThreeParameters() - ), - ActionRedirect("ActionWithMoreThanThreeParameters", - t => t.ShouldRedirectTo(typeof(ControllerResultTestController).GetMethod("ActionWithMoreThanThreeParameters")), - c => c.RedirectToActionWithMoreThanThreeParameters() - ), - }; - // Different ways that redirects to another controller can be asserted - private static readonly List OtherControllerRedirects = new List - { - c => c.ShouldRedirectTo(typeof(SomeOtherController).GetMethod("SomeAction")), - c => c.ShouldRedirectTo(c2 => c2.SomeAction()), - }; - #pragma warning restore 169 - #endregion - - #region Setup - public class RedirectToActionTestMetadata : Tuple>> - { - public RedirectToActionTestMetadata(string expectedMethodName, TestAction testCall, Expression> validControllerActionCall) - : base(expectedMethodName, testCall, validControllerActionCall) { } - } - private ControllerResultTestController _controller; - public delegate void TestAction(ControllerResultTest testClass); - private static Tuple ReturnType(TestAction a) - { - return new Tuple(typeof(T).Name, a); - } - private static RedirectToActionTestMetadata ActionRedirect(string s, TestAction a, Expression> c) - { - return new RedirectToActionTestMetadata(s, a, c); - } - - [SetUp] - public void Setup() - { - _controller = new ControllerResultTestController(); - } - #endregion - - #region General tests - [Test] - [TestCaseSource("ReturnTypes")] - public void Check_return_type(Tuple test) - { - var exception = Assert.Throws(() => - test.Item2(_controller.WithCallTo(c => c.RandomResult())) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected action result to be a {0}, but instead received a RandomResult.", test.Item1))); - } - - [Test] - [TestCaseSource("ReturnTypes")] - public void Check_null_return(Tuple test) - { - var exception = Assert.Throws(() => - test.Item2(_controller.WithCallTo(c => c.Null())) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Received null action result when expecting {0}.", test.Item1))); - } - - [Test] - public void Check_for_empty_result() - { - _controller.WithCallTo(c => c.EmptyResult()).ShouldReturnEmptyResult(); - } - #endregion - - #region Redirect tests - [Test] - public void Check_for_redirect_to_url() - { - _controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(ControllerResultTestController.RedirectUrl); - } - - [Test] - public void Check_for_redirect_to_invalid_url() - { - const string url = "http://validurl/"; - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(url) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, ControllerResultTestController.RedirectUrl))); - } - - [Test] - public void Check_for_redirect_to_route_name() - { - _controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(ControllerResultTestController.RouteName); - } - - [Test] - public void Check_for_redirect_to_invalid_route_name() - { - const string routeName = "ValidRoute"; - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(routeName) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", routeName, ControllerResultTestController.RouteName))); - } - - [Test] - [TestCaseSource("ActionRedirects")] - public void Check_for_redirect_to_action(RedirectToActionTestMetadata test) - { - test.Item2(_controller.WithCallTo(test.Item3)); - } - - [Test] - [TestCaseSource("ActionRedirects")] - public void Check_for_redirect_to_incorrect_controller(RedirectToActionTestMetadata test) - { - var exception = Assert.Throws(() => - test.Item2(_controller.WithCallTo(c => c.RedirectToAnotherController())) - ); - Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'ControllerResultTest', but instead was given a redirect to controller 'SomeOther'.")); - } - - [Test] - [TestCaseSource("ActionRedirects")] - public void Check_for_redirect_to_incorrect_action(RedirectToActionTestMetadata test) - { - var exception = Assert.Throws(() => - test.Item2(_controller.WithCallTo(c => c.RedirectToRandomResult())) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action 'RandomResult'.", test.Item1))); - } - - // todo: Test the route values expectations - - [Test] - [TestCaseSource("ActionRedirects")] - public void Check_for_redirect_to_empty_action(RedirectToActionTestMetadata test) - { - var exception = Assert.Throws(() => - test.Item2(_controller.WithCallTo(c => c.RedirectToRouteName())) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", test.Item1))); - } - - [Test] - [TestCaseSource("OtherControllerRedirects")] - public void Check_for_redirect_to_another_controller(TestAction action) - { - action(_controller.WithCallTo(c => c.RedirectToAnotherController())); - } - - [Test] - public void Check_for_redirect_to_incorrect_other_controller() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo(c => c.SomeAction()) - ); - Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'YetAnother', but instead was given a redirect to controller 'SomeOther'.")); - } - - [Test] - public void Check_for_redirect_to_incorrect_action_in_another_controller() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo(c => c.SomeOtherAction()) - ); - Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction', but instead was given a redirect to action 'SomeAction'.")); - } - - [Test] - public void Check_for_redirect_to_action_with_non_specified_controller() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RedirectToAnotherActionNoController()).ShouldRedirectTo(c => c.SomeOtherAction()) - ); - Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction' in 'SomeOther' controller, but instead was given redirect to action 'SomeAction' within the same controller.")); - } - #endregion - - #region View tests - [Test] - public void Check_for_default_view_or_partial() - { - _controller.WithCallTo(c => c.DefaultView()).ShouldRenderDefaultView(); - _controller.WithCallTo(c => c.DefaultView()).ShouldRenderView("DefaultView"); - _controller.WithCallTo(c => c.DefaultViewExplicit()).ShouldRenderDefaultView(); - _controller.WithCallTo(c => c.DefaultPartial()).ShouldRenderDefaultPartialView(); - _controller.WithCallTo(c => c.DefaultPartial()).ShouldRenderPartialView("DefaultPartial"); - _controller.WithCallTo(c => c.DefaultPartialExplicit()).ShouldRenderDefaultPartialView(); - } - - [Test] - public void Check_for_invalid_view_name_when_expecting_default_view() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RandomView()).ShouldRenderDefaultView() - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be 'RandomView', but instead was given '{0}'.", ControllerResultTestController.RandomViewName))); - } - - [Test] - public void Check_for_invalid_view_name_when_expecting_default_partial() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RandomPartial()).ShouldRenderDefaultPartialView() - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be 'RandomPartial', but instead was given '{0}'.", ControllerResultTestController.RandomViewName))); - } - - [Test] - public void Check_for_named_view_or_partial() - { - _controller.WithCallTo(c => c.NamedView()).ShouldRenderView(ControllerResultTestController.ViewName); - _controller.WithCallTo(c => c.NamedPartial()).ShouldRenderPartialView(ControllerResultTestController.PartialName); - } - - [Test] - public void Check_for_invalid_view_name() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RandomView()).ShouldRenderView(ControllerResultTestController.ViewName) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.ViewName, ControllerResultTestController.RandomViewName))); - } - - [Test] - public void Check_for_invalid_partial_name() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.RandomPartial()).ShouldRenderPartialView(ControllerResultTestController.PartialName) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName))); - } - #endregion - - #region File tests - - [Test] - public void Check_for_any_file_result() - { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(); - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(); - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(); - } - - [Test] - public void Check_for_any_file_result_and_check_content_type() - { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_any_file_result_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_content_result() - { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents(); - } - - [Test] - public void Check_for_file_content_result_and_check_binary_content() - { - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents); - } - - [Test] - public void Check_for_file_content_result_and_check_invalid_binary_content() - { - byte[] contents = { 1, 2 }; - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents)); - - Assert.True(exception.Message.StartsWith("Expected file contents to be equal to [")); - Assert.True(exception.Message.EndsWith("].")); - Assert.True(string.Join(", ", contents).All(exception.Message.Contains)); - Assert.True(string.Join(", ", ControllerResultTestController.BinaryFileContents).All(exception.Message.Contains)); - } - - [Test] - public void Check_for_file_content_result_and_check_binary_content_and_check_content_type() - { - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_content_result_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_content_result_and_check_invalid_binary_content_and_check_invalid_content_type() - { - byte[] contents = { 1, 2 }; - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType)); - - // Assert that the content type validation occurs before that of the actual contents. - Assert.That(exception.Message.Contains("content type")); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_contents() - { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent); - } - - [Test] - public void Check_for_file_content_result_and_check_invalid_textual_contents() - { - const string contents = "dummy contents"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_content_and_check_content_result() - { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_typet() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_type() - { - const string contents = "dummy content"; - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents, contentType)); - - // Assert that the content type validation occurs before that of the actual contents. - Assert.That(exception.Message.Contains("content type")); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding() - { - var encoding = Encoding.BigEndianUnicode; - - _controller.WithCallTo(c => c.TextualFile(encoding)) - .ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, encoding: encoding); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() - { - var encoding = Encoding.BigEndianUnicode; - - _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); - } - - [Test] - public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding() - { - Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); - } - - [Test] - public void Check_for_file_stream_result() - { - _controller.WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(); - } - - [Test] - public void Check_for_file_stream_result_and_check_stream_content() - { - _controller.WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_stream_content() - { - var buffer = new byte[] { 1, 2 }; - var stream = new MemoryStream(buffer); - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream)); - - var expected = string.Format("[{0}]", string.Join(", ", buffer)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyFileBuffer)); - 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)); - } - - [Test] - public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_content() - { - var buffer = new byte[] { 1, 2 }; - var stream = new MemoryStream(buffer); - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(stream)); - - var expected = string.Format("[{0}]", string.Join(", ", buffer)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); - 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)); - } - - [Test] - public void Check_for_file_stream_result_and_check_content_type() - { - _controller.WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents,ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format( - "Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_stream_content_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - var buffer = new byte[] { 1, 2 }; - var stream = new MemoryStream(buffer); - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream, contentType)); - - // Assert that the content type validation occurs before that of the actual contents. - Assert.That(exception.Message.Contains("content type")); - } - - [Test] - public void Check_for_file_stream_result_and_check_binary_content() - { - _controller.WithCallTo(c => c.BinaryStream()) - .ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_binary_content() - { - var content = new byte[] { 1, 2 }; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content)); - - var expected = string.Format("[{0}]", string.Join(", ", content)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); - 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)); - } - - [Test] - public void Check_for_file_stream_result_and_check_binary_content_and_check_content_type() - { - _controller.WithCallTo(c => c.BinaryStream()) - .ShouldRenderFileStream(ControllerResultTestController.BinaryStreamContents, ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_stream_result_and_check_binary_content_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", - contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_binary_content_and_check_invalid_content_type() - { - var content = new byte[] { 1, 2 }; - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content, contentType)); - - // Assert that the content type validation occurs before that of the actual contents. - Assert.That(exception.Message.Contains("content type")); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content() - { - _controller.WithCallTo(c => c.TextualStream()) - .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_textual_content() - { - const string contents = "dummy contents"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(contents)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content_and_check_content_type() - { - _controller.WithCallTo(c => c.TextualStream()) - .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", - contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_textual_content_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - const string content = "dummy contents"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(content, contentType)); - - // Assert that the content type validation occurs before that of the actual contents. - Assert.That(exception.Message.Contains("content type")); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding() - { - var encoding = Encoding.BigEndianUnicode; - - _controller.WithCallTo(c => c.TextualStream(encoding)) - .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: encoding); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() - { - var encoding = Encoding.BigEndianUnicode; - - _controller.WithCallTo(c => c.TextualStream(encoding)) - .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); - } - - [Test] - public void Check_for_file_stream_result_and_check_textual_content_using_invalid_given_char_encoding_and_check_content_type() - { - Assert.Throws(() => - _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: Encoding.BigEndianUnicode)); - } - - [Test] - public void Check_for_file_path_result() - { - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(); - } - - [Test] - public void Check_for_file_path_result_and_check_file_name() - { - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName); - } - - [Test] - public void Check_for_file_path_result_and_check_invalid_file_name() - { - const string name = "dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", name, ControllerResultTestController.FileName))); - } - - [Test] - public void Check_for_file_path_result_and_check_file_name_and_check_content_type() - { - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName, ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_path_result_and_check_file_name_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName, contentType)); - - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); - } - - [Test] - public void Check_for_file_path_result_and_check_invalid_file_name_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - const string name = "dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType)); - - // Assert that the content type validation occurs before that of the file name. - Assert.That(exception.Message.Contains("content type")); - } - - #endregion - - #region HTTP Status tests - [Test] - public void Check_for_http_not_found() - { - _controller.WithCallTo(c => c.NotFound()).ShouldGiveHttpStatus(HttpStatusCode.NotFound); - } - - [Test] - public void Check_for_http_status() - { - _controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code); - } - - [Test] - public void Check_for_invalid_http_status() - { - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code+1) - ); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", ControllerResultTestController.Code + 1, ControllerResultTestController.Code))); - } - #endregion - - #region Json tests - [Test] - 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")); - } - - [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/ControllerResultTestTests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ControllerResultTestTests.cs new file mode 100644 index 0000000..eb42c3a --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ControllerResultTestTests.cs @@ -0,0 +1,82 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Web.Mvc; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + [TestFixture] + partial class ControllerResultTestShould + { + private ControllerResultTestController _controller; + + [SetUp] + public void Setup() + { + _controller = new ControllerResultTestController(); + } + + #region General Tests + // Expected action return types for the different types of assertions + private static readonly List> ReturnTypes = new List> + { + ReturnType(t => t.ShouldReturnEmptyResult()), + ReturnType(t => t.ShouldRedirectTo("")), + ReturnType(t => t.ShouldRedirectTo(c => c.EmptyResult)), + ReturnType(t => t.ShouldRedirectTo(c => c.SomeAction())), + ReturnType(t => t.ShouldRenderView("")), + ReturnType(t => t.ShouldRenderDefaultView()), + ReturnType(t => t.ShouldRenderPartialView("")), + ReturnType(t => t.ShouldRenderDefaultPartialView()), + ReturnType(t => t.ShouldRenderFileContents()), + ReturnType(t => t.ShouldRenderFileContents(new byte[0])), + ReturnType(t => t.ShouldRenderFileContents(new byte[0], "")), + ReturnType(t => t.ShouldRenderFileContents("")), + ReturnType(t => t.ShouldRenderFileContents("", "")), + ReturnType(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)), + ReturnType(t => t.ShouldRenderFilePath()), + ReturnType(t => t.ShouldRenderFilePath("")), + ReturnType(t => t.ShouldRenderFilePath("", "")), + ReturnType(t => t.ShouldRenderFileStream()), + ReturnType(t => t.ShouldRenderFileStream(new MemoryStream())), + ReturnType(t => t.ShouldRenderFileStream(contentType: "")), + ReturnType(t => t.ShouldRenderFileStream(new MemoryStream(), "")), + ReturnType(t => t.ShouldRenderFileStream(new byte[0])), + ReturnType(t => t.ShouldRenderFileStream(new byte[0], "")), + ReturnType(t => t.ShouldRenderFileStream("")), + ReturnType(t => t.ShouldRenderFileStream("", "")), + ReturnType(t => t.ShouldRenderFileStream("", "", Encoding.UTF8)), + 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)) + }; + + [Test] + [TestCaseSource("ReturnTypes")] + public void Check_return_type(Tuple test) + { + var exception = Assert.Throws(() => + test.Item2(_controller.WithCallTo(c => c.RandomResult())) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected action result to be a {0}, but instead received a RandomResult.", test.Item1))); + } + + [Test] + [TestCaseSource("ReturnTypes")] + public void Check_null_return(Tuple test) + { + var exception = Assert.Throws(() => + test.Item2(_controller.WithCallTo(c => c.Null())) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Received null action result when expecting {0}.", test.Item1))); + } + #endregion + } +} diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldGiveHttpStatusTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldGiveHttpStatusTests.cs new file mode 100644 index 0000000..78a4a4f --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldGiveHttpStatusTests.cs @@ -0,0 +1,30 @@ +using System.Net; +using NUnit.Framework; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [Test] + public void Check_for_http_not_found() + { + _controller.WithCallTo(c => c.NotFound()).ShouldGiveHttpStatus(HttpStatusCode.NotFound); + } + + [Test] + public void Check_for_http_status() + { + _controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code); + } + + [Test] + public void Check_for_invalid_http_status() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code + 1) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", ControllerResultTestController.Code + 1, ControllerResultTestController.Code))); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRedirectToTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRedirectToTests.cs new file mode 100644 index 0000000..3e8f57f --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRedirectToTests.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Web.Mvc; +using NUnit.Framework; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + // 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 + { + ActionRedirect("ActionWithNoParameters", + t => t.ShouldRedirectTo(c => c.ActionWithNoParameters), + c => c.RedirectToActionWithNoParameters() + ), + ActionRedirect("ActionWithOneParameter", + t => t.ShouldRedirectTo(c => c.ActionWithOneParameter), + c => c.RedirectToActionWithOneParameter() + ), + ActionRedirect("ActionWithOneParameter", + t => t.ShouldRedirectTo(c => c.ActionWithOneParameter), + c => c.RedirectToActionWithOneParameter() + ), + ActionRedirect("ActionWithTwoParameters", + t => t.ShouldRedirectTo(c => c.ActionWithTwoParameters), + c => c.RedirectToActionWithTwoParameters() + ), + ActionRedirect("ActionWithThreeParameters", + t => t.ShouldRedirectTo(c => c.ActionWithThreeParameters), + c => c.RedirectToActionWithThreeParameters() + ), + ActionRedirect("ActionWithNoParameters", + t => t.ShouldRedirectTo(c => c.ActionWithNoParameters()), + c => c.RedirectToActionWithNoParameters() + ), + ActionRedirect("ActionWithOneParameter", + t => t.ShouldRedirectTo(c => c.ActionWithOneParameter(0)), + c => c.RedirectToActionWithOneParameter() + ), + ActionRedirect("ActionWithTwoParameters", + t => t.ShouldRedirectTo(c => c.ActionWithTwoParameters(0, 0)), + c => c.RedirectToActionWithTwoParameters() + ), + ActionRedirect("ActionWithThreeParameters", + t => t.ShouldRedirectTo(c => c.ActionWithThreeParameters(0, 0, 0)), + c => c.RedirectToActionWithThreeParameters() + ), + ActionRedirect("ActionWithMoreThanThreeParameters", + t => t.ShouldRedirectTo(c => c.ActionWithMoreThanThreeParameters(0, 0, 0, 0)), + c => c.RedirectToActionWithMoreThanThreeParameters() + ), + ActionRedirect("ActionWithMoreThanThreeParameters", + t => t.ShouldRedirectTo(typeof(ControllerResultTestController).GetMethod("ActionWithMoreThanThreeParameters")), + c => c.RedirectToActionWithMoreThanThreeParameters() + ), + }; + + // Different ways that redirects to another controller can be asserted + private static readonly List OtherControllerRedirects = new List + { + c => c.ShouldRedirectTo(typeof(SomeOtherController).GetMethod("SomeAction")), + c => c.ShouldRedirectTo(c2 => c2.SomeAction()), + }; + + public class RedirectToActionTestMetadata : Tuple>> + { + public RedirectToActionTestMetadata(string expectedMethodName, TestAction testCall, Expression> validControllerActionCall) + : base(expectedMethodName, testCall, validControllerActionCall) { } + } + + public delegate void TestAction(ControllerResultTest testClass); + + private static Tuple ReturnType(TestAction a) + { + return new Tuple(typeof(T).Name, a); + } + + private static RedirectToActionTestMetadata ActionRedirect(string s, TestAction a, Expression> c) + { + return new RedirectToActionTestMetadata(s, a, c); + } + + [Test] + public void Check_for_redirect_to_url() + { + _controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(ControllerResultTestController.RedirectUrl); + } + + [Test] + public void Check_for_redirect_to_invalid_url() + { + const string url = "http://validurl/"; + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(url) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, ControllerResultTestController.RedirectUrl))); + } + + [Test] + public void Check_for_redirect_to_route_name() + { + _controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(ControllerResultTestController.RouteName); + } + + [Test] + public void Check_for_redirect_to_invalid_route_name() + { + const string routeName = "ValidRoute"; + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(routeName) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", routeName, ControllerResultTestController.RouteName))); + } + + [Test] + [TestCaseSource("ActionRedirects")] + public void Check_for_redirect_to_action(RedirectToActionTestMetadata test) + { + test.Item2(_controller.WithCallTo(test.Item3)); + } + + [Test] + [TestCaseSource("ActionRedirects")] + public void Check_for_redirect_to_incorrect_controller(RedirectToActionTestMetadata test) + { + var exception = Assert.Throws(() => + test.Item2(_controller.WithCallTo(c => c.RedirectToAnotherController())) + ); + Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'ControllerResultTest', but instead was given a redirect to controller 'SomeOther'.")); + } + + [Test] + [TestCaseSource("ActionRedirects")] + public void Check_for_redirect_to_incorrect_action(RedirectToActionTestMetadata test) + { + var exception = Assert.Throws(() => + test.Item2(_controller.WithCallTo(c => c.RedirectToRandomResult())) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action 'RandomResult'.", test.Item1))); + } + + // todo: Test the route values expectations + + [Test] + [TestCaseSource("ActionRedirects")] + public void Check_for_redirect_to_empty_action(RedirectToActionTestMetadata test) + { + var exception = Assert.Throws(() => + test.Item2(_controller.WithCallTo(c => c.RedirectToRouteName())) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", test.Item1))); + } + + [Test] + [TestCaseSource("OtherControllerRedirects")] + public void Check_for_redirect_to_another_controller(TestAction action) + { + action(_controller.WithCallTo(c => c.RedirectToAnotherController())); + } + + [Test] + public void Check_for_redirect_to_incorrect_other_controller() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo(c => c.SomeAction()) + ); + Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'YetAnother', but instead was given a redirect to controller 'SomeOther'.")); + } + + [Test] + public void Check_for_redirect_to_incorrect_action_in_another_controller() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo(c => c.SomeOtherAction()) + ); + Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction', but instead was given a redirect to action 'SomeAction'.")); + } + + [Test] + public void Check_for_redirect_to_action_with_non_specified_controller() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RedirectToAnotherActionNoController()).ShouldRedirectTo(c => c.SomeOtherAction()) + ); + Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction' in 'SomeOther' controller, but instead was given redirect to action 'SomeAction' within the same controller.")); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderFileTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderFileTests.cs new file mode 100644 index 0000000..3034cd9 --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderFileTests.cs @@ -0,0 +1,425 @@ +using System.IO; +using NUnit.Framework; +using System.Text; +using TestStack.FluentMVCTesting.Tests.TestControllers; +using System.Linq; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [Test] + public void Check_for_any_file_result() + { + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(); + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(); + } + + [Test] + public void Check_for_any_file_result_and_check_content_type() + { + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_any_file_result_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_content_result() + { + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents(); + } + + [Test] + public void Check_for_file_content_result_and_check_binary_content() + { + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents); + } + + [Test] + public void Check_for_file_content_result_and_check_invalid_binary_content() + { + byte[] contents = { 1, 2 }; + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents)); + + Assert.True(exception.Message.StartsWith("Expected file contents to be equal to [")); + Assert.True(exception.Message.EndsWith("].")); + Assert.True(string.Join(", ", contents).All(exception.Message.Contains)); + Assert.True(string.Join(", ", ControllerResultTestController.BinaryFileContents).All(exception.Message.Contains)); + } + + [Test] + public void Check_for_file_content_result_and_check_binary_content_and_check_content_type() + { + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_content_result_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_content_result_and_check_invalid_binary_content_and_check_invalid_content_type() + { + byte[] contents = { 1, 2 }; + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_contents() + { + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent); + } + + [Test] + public void Check_for_file_content_result_and_check_invalid_textual_contents() + { + const string contents = "dummy contents"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_content_and_check_content_result() + { + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_typet() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_type() + { + const string contents = "dummy content"; + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding() + { + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualFile(encoding)) + .ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, encoding: encoding); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() + { + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); + } + + [Test] + public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding() + { + Assert.Throws(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); + } + + [Test] + public void Check_for_file_stream_result() + { + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(); + } + + [Test] + public void Check_for_file_stream_result_and_check_stream_content() + { + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_stream_content() + { + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream)); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyFileBuffer)); + 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)); + } + + [Test] + public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_content() + { + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(stream)); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); + 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)); + } + + [Test] + public void Check_for_file_stream_result_and_check_content_type() + { + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_stream_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Check_for_file_stream_result_and_check_binary_content() + { + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_content() + { + var content = new byte[] { 1, 2 }; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content)); + + var expected = string.Format("[{0}]", string.Join(", ", content)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); + 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)); + } + + [Test] + public void Check_for_file_stream_result_and_check_binary_content_and_check_content_type() + { + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryStreamContents, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_binary_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", + contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_content_and_check_invalid_content_type() + { + var content = new byte[] { 1, 2 }; + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content() + { + _controller.WithCallTo(c => c.TextualStream()) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_textual_content() + { + const string contents = "dummy contents"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(contents)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_and_check_content_type() + { + _controller.WithCallTo(c => c.TextualStream()) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", + contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_textual_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + const string content = "dummy contents"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(content, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding() + { + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualStream(encoding)) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: encoding); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() + { + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualStream(encoding)) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_using_invalid_given_char_encoding_and_check_content_type() + { + Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: Encoding.BigEndianUnicode)); + } + + [Test] + public void Check_for_file_path_result() + { + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(); + } + + [Test] + public void Check_for_file_path_result_and_check_file_name() + { + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName); + } + + [Test] + public void Check_for_file_path_result_and_check_invalid_file_name() + { + const string name = "dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", name, ControllerResultTestController.FileName))); + } + + [Test] + public void Check_for_file_path_result_and_check_file_name_and_check_content_type() + { + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_path_result_and_check_file_name_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(ControllerResultTestController.FileName, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } + + [Test] + public void Check_for_file_path_result_and_check_invalid_file_name_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + const string name = "dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType)); + + // Assert that the content type validation occurs before that of the file name. + Assert.That(exception.Message.Contains("content type")); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderViewTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderViewTests.cs new file mode 100644 index 0000000..b57e01b --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldRenderViewTests.cs @@ -0,0 +1,62 @@ +using NUnit.Framework; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [Test] + public void Check_for_default_view_or_partial() + { + _controller.WithCallTo(c => c.DefaultView()).ShouldRenderDefaultView(); + _controller.WithCallTo(c => c.DefaultView()).ShouldRenderView("DefaultView"); + _controller.WithCallTo(c => c.DefaultViewExplicit()).ShouldRenderDefaultView(); + _controller.WithCallTo(c => c.DefaultPartial()).ShouldRenderDefaultPartialView(); + _controller.WithCallTo(c => c.DefaultPartial()).ShouldRenderPartialView("DefaultPartial"); + _controller.WithCallTo(c => c.DefaultPartialExplicit()).ShouldRenderDefaultPartialView(); + } + + [Test] + public void Check_for_invalid_view_name_when_expecting_default_view() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RandomView()).ShouldRenderDefaultView() + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be 'RandomView', but instead was given '{0}'.", ControllerResultTestController.RandomViewName))); + } + + [Test] + public void Check_for_invalid_view_name_when_expecting_default_partial() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RandomPartial()).ShouldRenderDefaultPartialView() + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be 'RandomPartial', but instead was given '{0}'.", ControllerResultTestController.RandomViewName))); + } + + [Test] + public void Check_for_named_view_or_partial() + { + _controller.WithCallTo(c => c.NamedView()).ShouldRenderView(ControllerResultTestController.ViewName); + _controller.WithCallTo(c => c.NamedPartial()).ShouldRenderPartialView(ControllerResultTestController.PartialName); + } + + [Test] + public void Check_for_invalid_view_name() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RandomView()).ShouldRenderView(ControllerResultTestController.ViewName) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.ViewName, ControllerResultTestController.RandomViewName))); + } + + [Test] + public void Check_for_invalid_partial_name() + { + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.RandomPartial()).ShouldRenderPartialView(ControllerResultTestController.PartialName) + ); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName))); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnContentTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnContentTests.cs new file mode 100644 index 0000000..d4e2dca --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnContentTests.cs @@ -0,0 +1,84 @@ +using NUnit.Framework; +using System.Text; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [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")); + } + + [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))); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnEmptyResultTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnEmptyResultTests.cs new file mode 100644 index 0000000..8990602 --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnEmptyResultTests.cs @@ -0,0 +1,13 @@ +using NUnit.Framework; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [Test] + public void Check_for_empty_result() + { + _controller.WithCallTo(c => c.EmptyResult()).ShouldReturnEmptyResult(); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnJsonTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnJsonTests.cs new file mode 100644 index 0000000..fdf420d --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests/ShouldReturnJsonTests.cs @@ -0,0 +1,14 @@ +using NUnit.Framework; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + partial class ControllerResultTestShould + { + [Test] + 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))); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj b/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj index fc13805..5129022 100644 --- a/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj +++ b/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj @@ -82,11 +82,18 @@ + + + + + + + - + diff --git a/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj.DotSettings b/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj.DotSettings new file mode 100644 index 0000000..41697fc --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs deleted file mode 100644 index 38a778a..0000000 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ /dev/null @@ -1,436 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Net; -using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; -using System.Web.Mvc; -using System.Web.Routing; - -namespace TestStack.FluentMVCTesting -{ - public class ControllerResultTest where T : Controller - { - #region Private properties and methods - private readonly T _controller; - private readonly string _actionName; - private readonly ActionResult _actionResult; - - private void ValidateActionReturnType() where TActionResult : ActionResult - { - var castedActionResult = _actionResult as TActionResult; - - if (_actionResult == null) - throw new ActionResultAssertionException(string.Format("Received null action result when expecting {0}.", typeof(TActionResult).Name)); - - if (castedActionResult == null) - throw new ActionResultAssertionException( - string.Format("Expected action result to be a {0}, but instead received a {1}.", - typeof(TActionResult).Name, _actionResult.GetType().Name - ) - ); - } - #endregion - - #region Ctor - - public ControllerResultTest(T controller, string actionName, ActionResult actionResult) - { - _controller = controller; - _actionName = actionName; - _actionResult = actionResult; - } - - #endregion - - #region Empty Result - - public void ShouldReturnEmptyResult() - { - ValidateActionReturnType(); - } - - #endregion - - #region Redirects - - public void ShouldRedirectTo(string url) - { - ValidateActionReturnType(); - var redirectResult = (RedirectResult)_actionResult; - - if (redirectResult.Url != url) - throw new ActionResultAssertionException(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, redirectResult.Url)); - } - - public RouteValueDictionary ShouldRedirectToRoute(string route) - { - ValidateActionReturnType(); - var redirectResult = (RedirectToRouteResult)_actionResult; - - if (redirectResult.RouteName != route) - throw new ActionResultAssertionException(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", route, redirectResult.RouteName)); - - return redirectResult.RouteValues; - } - - public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) - { - return ShouldRedirectTo(actionRedirectedTo(_controller).Method); - } - - public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) - { - return ShouldRedirectTo(actionRedirectedTo(_controller).Method); - } - - public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) - { - return ShouldRedirectTo(actionRedirectedTo(_controller).Method); - } - - public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) - { - return ShouldRedirectTo(actionRedirectedTo(_controller).Method); - } - - public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) - { - return ShouldRedirectTo(actionRedirectedTo(_controller).Method); - } - - public RouteValueDictionary ShouldRedirectTo(Expression> actionRedirectedTo) - { - var methodCall = (MethodCallExpression) actionRedirectedTo.Body; - return ShouldRedirectTo(methodCall.Method); - } - - public RouteValueDictionary ShouldRedirectTo(MethodInfo method, RouteValueDictionary expectedValues = null) - { - ValidateActionReturnType(); - - var controllerName = new Regex(@"Controller$").Replace(typeof(T).Name, ""); - var actionName = method.Name; - var redirectResult = (RedirectToRouteResult)_actionResult; - - if (redirectResult.RouteValues.ContainsKey("Controller") && redirectResult.RouteValues["Controller"].ToString() != controllerName) - throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"])); - - if (!redirectResult.RouteValues.ContainsKey("Action")) - throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", actionName)); - - if (redirectResult.RouteValues["Action"].ToString() != actionName) - throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"])); - - if (expectedValues == null) - return redirectResult.RouteValues; - - foreach (var expectedRouteValue in expectedValues) - { - object actualValue; - if (!redirectResult.RouteValues.TryGetValue(expectedRouteValue.Key, out actualValue)) - { - throw new ActionResultAssertionException(string.Format("Expected redirect to have parameter '{0}', but it did not.", expectedRouteValue.Key)); - } - if (actualValue.ToString() != expectedRouteValue.Value.ToString()) - { - throw new ActionResultAssertionException( - string.Format("Expected parameter '{0}' to have value '{1}', but instead was given value '{2}'." - , expectedRouteValue.Key - , expectedRouteValue.Value - , actualValue - )); - } - } - - return redirectResult.RouteValues; - } - - public RouteValueDictionary ShouldRedirectTo(Expression> actionRedirectedTo) where TController : Controller - { - var methodCall = (MethodCallExpression)actionRedirectedTo.Body; - return ShouldRedirectTo(methodCall.Method); - } - - public RouteValueDictionary ShouldRedirectTo(MethodInfo methodInfo) where TController : Controller - { - ValidateActionReturnType(); - - var controllerName = new Regex(@"Controller$").Replace(typeof(TController).Name, ""); - var actionName = methodInfo.Name; - - var redirectResult = (RedirectToRouteResult)_actionResult; - - if (redirectResult.RouteValues["Controller"] == null) - throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}' in '{1}' controller, but instead was given redirect to action '{2}' within the same controller.", actionName, controllerName, redirectResult.RouteValues["Action"])); - - if (redirectResult.RouteValues["Controller"].ToString() != controllerName) - throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"])); - - if (redirectResult.RouteValues["Action"].ToString() != actionName) - throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"])); - - return redirectResult.RouteValues; - } - - #endregion - - #region View Results - - private ViewResultTest ShouldRenderViewResult(string viewName) where TViewResult : ViewResultBase - { - ValidateActionReturnType(); - - var viewResult = (TViewResult)_actionResult; - - if (viewResult.ViewName != viewName && (viewName != _actionName || viewResult.ViewName != "")) - { - throw new ActionResultAssertionException(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", viewName, viewResult.ViewName == "" ? _actionName : viewResult.ViewName)); - } - - return new ViewResultTest(viewResult, _controller); - } - - public ViewResultTest ShouldRenderView(string viewName) - { - return ShouldRenderViewResult(viewName); - } - - public ViewResultTest ShouldRenderPartialView(string viewName) - { - return ShouldRenderViewResult(viewName); - } - - public ViewResultTest ShouldRenderDefaultView() - { - return ShouldRenderView(_actionName); - } - - public ViewResultTest ShouldRenderDefaultPartialView() - { - return ShouldRenderPartialView(_actionName); - } - - #endregion - - #region File Results - - private static void EnsureContentTypeIsSame(string actual, string expected) - { - 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)); - } - } - - private static byte[] ConvertStreamToArray(Stream stream) - { - using (var memoryStream = new MemoryStream()) - { - stream.CopyTo(memoryStream); - stream.Position = 0; - return memoryStream.ToArray(); - } - } - - public FileResult ShouldRenderAnyFile(string contentType = null) - { - ValidateActionReturnType(); - var fileResult = (FileResult)_actionResult; - - EnsureContentTypeIsSame(fileResult.ContentType, contentType); - - return fileResult; - } - - public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null) - { - ValidateActionReturnType(); - var fileResult = (FileContentResult) _actionResult; - - EnsureContentTypeIsSame(fileResult.ContentType, contentType); - - if (contents != null && !fileResult.FileContents.SequenceEqual(contents)) - { - throw new ActionResultAssertionException(string.Format( - "Expected file contents to be equal to [{0}], but instead was given [{1}].", - string.Join(", ", contents), - string.Join(", ", fileResult.FileContents))); - } - - return fileResult; - } - - public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null) - { - ValidateActionReturnType(); - var fileResult = (FileContentResult)_actionResult; - - 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)); - } - - return fileResult; - } - - public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) - { - var reconstitutedStream = new MemoryStream(content); - return ShouldRenderFileStream(reconstitutedStream, contentType); - } - - public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) - { - ValidateActionReturnType(); - var fileResult = (FileStreamResult)_actionResult; - - EnsureContentTypeIsSame(fileResult.ContentType, contentType); - - if (stream != null) - { - byte[] expected = ConvertStreamToArray(stream); - byte[] actual = ConvertStreamToArray(fileResult.FileStream); - - if (!expected.SequenceEqual(actual)) - { - throw new ActionResultAssertionException(string.Format( - "Expected file contents to be equal to [{0}], but instead was given [{1}].", - string.Join(", ", expected), - string.Join(", ", actual))); - } - } - - return fileResult; - } - - public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null) - { - ValidateActionReturnType(); - var fileResult = (FileStreamResult)_actionResult; - - EnsureContentTypeIsSame(fileResult.ContentType, contentType); - - if (encoding == null) - encoding = Encoding.UTF8; - - var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream)); - if (contents != reconstitutedText) - { - throw new ActionResultAssertionException(string.Format( - "Expected file contents to be \"{0}\", but instead was \"{1}\".", - contents, - reconstitutedText)); - } - - return fileResult; - } - - public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null) - { - ValidateActionReturnType(); - var fileResult = (FilePathResult)_actionResult; - - 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)); - } - - return fileResult; - } - - #endregion - - #region Http Status - - public void ShouldGiveHttpStatus() - { - ValidateActionReturnType(); - } - - public void ShouldGiveHttpStatus(int status) - { - ValidateActionReturnType(); - - var statusCodeResult = (HttpStatusCodeResult)_actionResult; - - if (statusCodeResult.StatusCode != status) - throw new ActionResultAssertionException(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", status, statusCodeResult.StatusCode)); - } - - public void ShouldGiveHttpStatus(HttpStatusCode status) - { - ShouldGiveHttpStatus((int) status); - } - - #endregion - - #region JSON - public void ShouldReturnJson() - { - ValidateActionReturnType(); - } - - public void ShouldReturnJson(Action assertion) - { - ValidateActionReturnType(); - var jsonResult = (JsonResult)_actionResult; - 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 != null ? contentResult.ContentEncoding.EncodingName : "null")); - } - - return contentResult; - } - - #endregion - } -} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ControllerResultTest.cs new file mode 100644 index 0000000..bb3ee35 --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ControllerResultTest.cs @@ -0,0 +1,33 @@ +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest where T : Controller + { + private readonly T _controller; + private readonly string _actionName; + private readonly ActionResult _actionResult; + + private void ValidateActionReturnType() where TActionResult : ActionResult + { + var castedActionResult = _actionResult as TActionResult; + + if (_actionResult == null) + throw new ActionResultAssertionException(string.Format("Received null action result when expecting {0}.", typeof(TActionResult).Name)); + + if (castedActionResult == null) + throw new ActionResultAssertionException( + string.Format("Expected action result to be a {0}, but instead received a {1}.", + typeof(TActionResult).Name, _actionResult.GetType().Name + ) + ); + } + + public ControllerResultTest(T controller, string actionName, ActionResult actionResult) + { + _controller = controller; + _actionName = actionName; + _actionResult = actionResult; + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldGiveHttpStatus.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldGiveHttpStatus.cs new file mode 100644 index 0000000..b29acad --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldGiveHttpStatus.cs @@ -0,0 +1,28 @@ +using System.Net; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + public void ShouldGiveHttpStatus() + { + ValidateActionReturnType(); + } + + public void ShouldGiveHttpStatus(int status) + { + ValidateActionReturnType(); + + var statusCodeResult = (HttpStatusCodeResult)_actionResult; + + if (statusCodeResult.StatusCode != status) + throw new ActionResultAssertionException(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", status, statusCodeResult.StatusCode)); + } + + public void ShouldGiveHttpStatus(HttpStatusCode status) + { + ShouldGiveHttpStatus((int)status); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRedirectTo.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRedirectTo.cs new file mode 100644 index 0000000..0e8899f --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRedirectTo.cs @@ -0,0 +1,131 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; +using System.Text.RegularExpressions; +using System.Web.Mvc; +using System.Web.Routing; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + public void ShouldRedirectTo(string url) + { + ValidateActionReturnType(); + var redirectResult = (RedirectResult)_actionResult; + + if (redirectResult.Url != url) + throw new ActionResultAssertionException(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, redirectResult.Url)); + } + + public RouteValueDictionary ShouldRedirectToRoute(string route) + { + ValidateActionReturnType(); + var redirectResult = (RedirectToRouteResult)_actionResult; + + if (redirectResult.RouteName != route) + throw new ActionResultAssertionException(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", route, redirectResult.RouteName)); + + return redirectResult.RouteValues; + } + + public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) + { + return ShouldRedirectTo(actionRedirectedTo(_controller).Method); + } + + public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) + { + return ShouldRedirectTo(actionRedirectedTo(_controller).Method); + } + + public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) + { + return ShouldRedirectTo(actionRedirectedTo(_controller).Method); + } + + public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) + { + return ShouldRedirectTo(actionRedirectedTo(_controller).Method); + } + + public RouteValueDictionary ShouldRedirectTo(Func> actionRedirectedTo) + { + return ShouldRedirectTo(actionRedirectedTo(_controller).Method); + } + + public RouteValueDictionary ShouldRedirectTo(Expression> actionRedirectedTo) + { + var methodCall = (MethodCallExpression)actionRedirectedTo.Body; + return ShouldRedirectTo(methodCall.Method); + } + + public RouteValueDictionary ShouldRedirectTo(MethodInfo method, RouteValueDictionary expectedValues = null) + { + ValidateActionReturnType(); + + var controllerName = new Regex(@"Controller$").Replace(typeof(T).Name, ""); + var actionName = method.Name; + var redirectResult = (RedirectToRouteResult)_actionResult; + + if (redirectResult.RouteValues.ContainsKey("Controller") && redirectResult.RouteValues["Controller"].ToString() != controllerName) + throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"])); + + if (!redirectResult.RouteValues.ContainsKey("Action")) + throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", actionName)); + + if (redirectResult.RouteValues["Action"].ToString() != actionName) + throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"])); + + if (expectedValues == null) + return redirectResult.RouteValues; + + foreach (var expectedRouteValue in expectedValues) + { + object actualValue; + if (!redirectResult.RouteValues.TryGetValue(expectedRouteValue.Key, out actualValue)) + { + throw new ActionResultAssertionException(string.Format("Expected redirect to have parameter '{0}', but it did not.", expectedRouteValue.Key)); + } + if (actualValue.ToString() != expectedRouteValue.Value.ToString()) + { + throw new ActionResultAssertionException( + string.Format("Expected parameter '{0}' to have value '{1}', but instead was given value '{2}'." + , expectedRouteValue.Key + , expectedRouteValue.Value + , actualValue + )); + } + } + + return redirectResult.RouteValues; + } + + public RouteValueDictionary ShouldRedirectTo(Expression> actionRedirectedTo) where TController : Controller + { + var methodCall = (MethodCallExpression)actionRedirectedTo.Body; + return ShouldRedirectTo(methodCall.Method); + } + + public RouteValueDictionary ShouldRedirectTo(MethodInfo methodInfo) where TController : Controller + { + ValidateActionReturnType(); + + var controllerName = new Regex(@"Controller$").Replace(typeof(TController).Name, ""); + var actionName = methodInfo.Name; + + var redirectResult = (RedirectToRouteResult)_actionResult; + + if (redirectResult.RouteValues["Controller"] == null) + throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}' in '{1}' controller, but instead was given redirect to action '{2}' within the same controller.", actionName, controllerName, redirectResult.RouteValues["Action"])); + + if (redirectResult.RouteValues["Controller"].ToString() != controllerName) + throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"])); + + if (redirectResult.RouteValues["Action"].ToString() != actionName) + throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"])); + + return redirectResult.RouteValues; + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderFile.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderFile.cs new file mode 100644 index 0000000..1db0626 --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderFile.cs @@ -0,0 +1,150 @@ +using System.IO; +using System.Linq; +using System.Text; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + private static void EnsureContentTypeIsSame(string actual, string expected) + { + 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)); + } + } + + private static byte[] ConvertStreamToArray(Stream stream) + { + using (var memoryStream = new MemoryStream()) + { + stream.CopyTo(memoryStream); + stream.Position = 0; + return memoryStream.ToArray(); + } + } + + public FileResult ShouldRenderAnyFile(string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileResult)_actionResult; + + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + + return fileResult; + } + + public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileContentResult)_actionResult; + + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + + if (contents != null && !fileResult.FileContents.SequenceEqual(contents)) + { + throw new ActionResultAssertionException(string.Format( + "Expected file contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", contents), + string.Join(", ", fileResult.FileContents))); + } + + return fileResult; + } + + public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null) + { + ValidateActionReturnType(); + var fileResult = (FileContentResult)_actionResult; + + 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)); + } + + return fileResult; + } + + public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) + { + var reconstitutedStream = new MemoryStream(content); + return ShouldRenderFileStream(reconstitutedStream, contentType); + } + + public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileStreamResult)_actionResult; + + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + + if (stream != null) + { + byte[] expected = ConvertStreamToArray(stream); + byte[] actual = ConvertStreamToArray(fileResult.FileStream); + + if (!expected.SequenceEqual(actual)) + { + throw new ActionResultAssertionException(string.Format( + "Expected file contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", expected), + string.Join(", ", actual))); + } + } + + return fileResult; + } + + public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null) + { + ValidateActionReturnType(); + var fileResult = (FileStreamResult)_actionResult; + + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + + if (encoding == null) + encoding = Encoding.UTF8; + + var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream)); + if (contents != reconstitutedText) + { + throw new ActionResultAssertionException(string.Format( + "Expected file contents to be \"{0}\", but instead was \"{1}\".", + contents, + reconstitutedText)); + } + + return fileResult; + } + + public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FilePathResult)_actionResult; + + 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)); + } + + return fileResult; + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderView.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderView.cs new file mode 100644 index 0000000..ee8188f --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldRenderView.cs @@ -0,0 +1,41 @@ +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + private ViewResultTest ShouldRenderViewResult(string viewName) where TViewResult : ViewResultBase + { + ValidateActionReturnType(); + + var viewResult = (TViewResult)_actionResult; + + if (viewResult.ViewName != viewName && (viewName != _actionName || viewResult.ViewName != "")) + { + throw new ActionResultAssertionException(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", viewName, viewResult.ViewName == "" ? _actionName : viewResult.ViewName)); + } + + return new ViewResultTest(viewResult, _controller); + } + + public ViewResultTest ShouldRenderView(string viewName) + { + return ShouldRenderViewResult(viewName); + } + + public ViewResultTest ShouldRenderPartialView(string viewName) + { + return ShouldRenderViewResult(viewName); + } + + public ViewResultTest ShouldRenderDefaultView() + { + return ShouldRenderView(_actionName); + } + + public ViewResultTest ShouldRenderDefaultPartialView() + { + return ShouldRenderPartialView(_actionName); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnContent.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnContent.cs new file mode 100644 index 0000000..0d38e05 --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnContent.cs @@ -0,0 +1,40 @@ +using System.Text; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + 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 != null ? contentResult.ContentEncoding.EncodingName : "null")); + } + + return contentResult; + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnEmptyResult.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnEmptyResult.cs new file mode 100644 index 0000000..0df9206 --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnEmptyResult.cs @@ -0,0 +1,12 @@ +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + public void ShouldReturnEmptyResult() + { + ValidateActionReturnType(); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnJson.cs b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnJson.cs new file mode 100644 index 0000000..acd1139 --- /dev/null +++ b/TestStack.FluentMvcTesting/ControllerResultTest/ShouldReturnJson.cs @@ -0,0 +1,20 @@ +using System; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public partial class ControllerResultTest + { + public void ShouldReturnJson() + { + ValidateActionReturnType(); + } + + public void ShouldReturnJson(Action assertion) + { + ValidateActionReturnType(); + var jsonResult = (JsonResult)_actionResult; + assertion(jsonResult.Data); + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/Exceptions.cs b/TestStack.FluentMvcTesting/Exceptions.cs index e3b36e5..76cf107 100644 --- a/TestStack.FluentMvcTesting/Exceptions.cs +++ b/TestStack.FluentMvcTesting/Exceptions.cs @@ -2,7 +2,6 @@ namespace TestStack.FluentMVCTesting { - public class TempDataAssertionException : Exception { public TempDataAssertionException(string message) : base(message) { } diff --git a/TestStack.FluentMvcTesting/RouteValueDictionaryExtension.cs b/TestStack.FluentMvcTesting/RouteValueDictionaryExtension.cs index f94e87e..d55f0cb 100644 --- a/TestStack.FluentMvcTesting/RouteValueDictionaryExtension.cs +++ b/TestStack.FluentMvcTesting/RouteValueDictionaryExtension.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Web.Routing; +using System.Web.Routing; namespace TestStack.FluentMVCTesting { diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj index 3fae519..553b197 100644 --- a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj @@ -76,9 +76,16 @@ + + + + + + + - + diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj.DotSettings b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj.DotSettings new file mode 100644 index 0000000..3af789e --- /dev/null +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file