diff --git a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj index b6989a6..9fcc5c4 100644 --- a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj +++ b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj @@ -88,6 +88,9 @@ ModelTest.cs + + TempDataResultTest.cs + ViewResultTest.cs diff --git a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs index 685aa3e..21cd3b8 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.IO; +using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; namespace TestStack.FluentMVCTesting.Tests @@ -41,5 +42,109 @@ public void Throw_exception_for_child_action_call_to_non_child_action() var exception = Assert.Throws(() => _controller.WithCallToChild(c => c.SomeAction())); Assert.That(exception.Message, Is.EqualTo("Expected action SomeAction of controller ControllerExtensionsController to be a child action, but it didn't have the ChildActionOnly attribute.")); } + + [Test] + public void Check_for_existent_temp_data_property() + { + const string key = ""; + _controller.TempData[key] = ""; + + _controller.ShouldHaveTempDataProperty(key); + } + + [Test] + public void Check_for_non_existent_temp_data_property() + { + const string key = ""; + + var exception = Assert.Throws(() => + _controller.ShouldHaveTempDataProperty(key)); + + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value() + { + const string key = ""; + const int value = 10; + _controller.TempData[key] = value; + + _controller.ShouldHaveTempDataProperty(key, value); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_invalid_value() + { + const string key = ""; + const int actualValue = 0; + const int expectedValue = 1; + _controller.TempData[key] = actualValue; + + var exception = Assert.Throws(() => + _controller.ShouldHaveTempDataProperty(key, expectedValue)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types() + { + const string key = ""; + const int actualValue = 0; + const string expectedValue = "one"; + _controller.TempData[key] = actualValue; + + var exception = Assert.Throws(() => + _controller.ShouldHaveTempDataProperty(key, expectedValue)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value to be of type {0}, but instead was {1}.", expectedValue.GetType().FullName, actualValue.GetType().FullName))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality() + { + const string key = ""; + MemoryStream expectedValue = new MemoryStream(); + _controller.TempData[key] = expectedValue; + + _controller.ShouldHaveTempDataProperty(key, expectedValue); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate() + { + const string key = ""; + const int value = 1; + _controller.TempData[key] = value; + + _controller + .ShouldHaveTempDataProperty(key, x => x == value); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate() + { + const string key = ""; + _controller.TempData[key] = 1; + + var exception = Assert.Throws(() => + _controller.ShouldHaveTempDataProperty(key, x => x == 0)); + + Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed.")); + } + + [Test] + public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate() + { + const string key = ""; + + var exception = Assert.Throws(() => + _controller.ShouldHaveTempDataProperty(key, x => x == 0)); + + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key))); + } } } diff --git a/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs b/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs new file mode 100644 index 0000000..d97f0f9 --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs @@ -0,0 +1,123 @@ +using System.IO; +using NUnit.Framework; +using TestStack.FluentMVCTesting.Tests.TestControllers; + +namespace TestStack.FluentMVCTesting.Tests +{ + [TestFixture] + public class TempDataResultTestShould + { + private ControllerExtensionsController _controller; + private TempDataResultTest _tempDataTest; + + [SetUp] + public void Setup() + { + _controller = new ControllerExtensionsController(); + _tempDataTest = new TempDataResultTest(_controller); + } + + [Test] + public void Check_for_existent_temp_data_property() + { + const string key = ""; + _controller.TempData[key] = ""; + + _tempDataTest.AndShouldHaveTempDataProperty(key); + } + + [Test] + public void Check_for_non_existent_temp_data_property() + { + const string key = ""; + + var exception = Assert.Throws(() => + _tempDataTest.AndShouldHaveTempDataProperty(key)); + + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value() + { + const string key = ""; + const int value = 10; + _controller.TempData[key] = value; + + _tempDataTest.AndShouldHaveTempDataProperty(key, value); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_invalid_value() + { + const string key = ""; + const int actualValue = 0; + const int expectedValue = 1; + _controller.TempData[key] = actualValue; + + var exception = Assert.Throws(() => + _tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types() + { + const string key = ""; + const int actualValue = 0; + const string expectedValue = "one"; + _controller.TempData[key] = actualValue; + + var exception = Assert.Throws(() => + _tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value to be of type {0}, but instead was {1}.", expectedValue.GetType().FullName, actualValue.GetType().FullName))); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality() + { + const string key = ""; + MemoryStream expectedValue = new MemoryStream(); + _controller.TempData[key] = expectedValue; + + _tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate() + { + const string key = ""; + const int value = 1; + _controller.TempData[key] = value; + + _tempDataTest.AndShouldHaveTempDataProperty(key, x => x == value); + } + + [Test] + public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate() + { + const string key = ""; + _controller.TempData[key] = 1; + + var exception = Assert.Throws(() => + _tempDataTest.AndShouldHaveTempDataProperty(key, x => x == 0)); + + Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed.")); + } + + [Test] + public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate() + { + const string key = ""; + + var exception = Assert.Throws(() => + _tempDataTest.AndShouldHaveTempDataProperty(key, x => x == 0)); + + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key))); + } + } +} \ 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 052bffa..831b7d1 100644 --- a/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj +++ b/TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj @@ -79,6 +79,7 @@ + @@ -106,4 +107,4 @@ --> - + \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index a3f9948..efad62b 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -59,5 +59,50 @@ public static ControllerResultTest WithCallToChild(this T control return controller.WithCallTo(actionCall); } + + public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null) + { + var actual = controller.TempData[key]; + + if (actual == null) + { + throw new TempDataAssertionException(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key)); + } + + if (value != null && actual.GetType() != value.GetType()) + { + throw new TempDataAssertionException(string.Format( + "Expected value to be of type {0}, but instead was {1}.", + value.GetType().FullName, + actual.GetType().FullName)); + } + + if (value != null && !value.Equals(actual)) + { + throw new TempDataAssertionException(string.Format( + "Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual)); + } + + return new TempDataResultTest(controller); + } + + public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, Func predicate) + { + var actual = controller.TempData[key]; + + if (actual == null) + { + throw new TempDataAssertionException(string.Format( + "Expected TempData to have a non-null value with key \"{0}\", but none found.", key)); + } + + if (!predicate((TValue)actual)) + { + throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed."); + } + + return new TempDataResultTest(controller); + } } } diff --git a/TestStack.FluentMvcTesting/Exceptions.cs b/TestStack.FluentMvcTesting/Exceptions.cs index ca901bf..e3b36e5 100644 --- a/TestStack.FluentMvcTesting/Exceptions.cs +++ b/TestStack.FluentMvcTesting/Exceptions.cs @@ -3,6 +3,11 @@ namespace TestStack.FluentMVCTesting { + public class TempDataAssertionException : Exception + { + public TempDataAssertionException(string message) : base(message) { } + } + public class ActionResultAssertionException : Exception { public ActionResultAssertionException(string message) : base(message) { } diff --git a/TestStack.FluentMvcTesting/TempDataResultTest.cs b/TestStack.FluentMvcTesting/TempDataResultTest.cs new file mode 100644 index 0000000..013378b --- /dev/null +++ b/TestStack.FluentMvcTesting/TempDataResultTest.cs @@ -0,0 +1,27 @@ +using System; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public class TempDataResultTest + { + private readonly ControllerBase _controller; + + public TempDataResultTest(ControllerBase controller) + { + _controller = controller; + } + + public TempDataResultTest AndShouldHaveTempDataProperty(string key, object value = null) + { + _controller.ShouldHaveTempDataProperty(key, value); + return this; + } + + public TempDataResultTest AndShouldHaveTempDataProperty(string key, Func predicate) + { + _controller.ShouldHaveTempDataProperty(key, predicate); + return this; + } + } +} \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj index 9f4f300..8f45b48 100644 --- a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj @@ -80,6 +80,7 @@ +