From b74b1e61dac088bd077954b41ae81eeb5ab78b8c Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Tue, 9 Sep 2014 17:51:24 +0100 Subject: [PATCH 01/10] Support for checking for temp data key existence. --- .../ControllerExtensionsTests.cs | 23 ++++++++++++++++++- .../ControllerExtensions.cs | 11 +++++++++ TestStack.FluentMvcTesting/Exceptions.cs | 5 ++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs index 685aa3e..aaea61b 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System; +using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; namespace TestStack.FluentMVCTesting.Tests @@ -41,5 +42,25 @@ 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))); + } } } diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index a3f9948..e3292a9 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -59,5 +59,16 @@ public static ControllerResultTest WithCallToChild(this T control return controller.WithCallTo(actionCall); } + + public static void ShouldHaveTempDataProperty(this Controller controller, string key) + { + 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)); + } + } } } 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) { } From 3dcdc2d64539bf37ffa80505dbcbe388454f39d0 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 01:39:27 +0100 Subject: [PATCH 02/10] Support for checking for temp data value. --- .../ControllerExtensionsTests.cs | 51 ++++++++++++++++++- .../ControllerExtensions.cs | 20 +++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs index aaea61b..2a21e7d 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs @@ -1,4 +1,4 @@ -using System; +using System.IO; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; @@ -56,11 +56,60 @@ public void Check_for_existent_temp_data_property() 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); + } } } diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index e3292a9..ff33fdb 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; +using System.Reflection.Emit; using System.Threading.Tasks; using System.Web.Mvc; @@ -60,7 +62,7 @@ public static ControllerResultTest WithCallToChild(this T control return controller.WithCallTo(actionCall); } - public static void ShouldHaveTempDataProperty(this Controller controller, string key) + public static void ShouldHaveTempDataProperty(this Controller controller, string key, object value = null) { var actual = controller.TempData[key]; @@ -69,6 +71,22 @@ public static void ShouldHaveTempDataProperty(this Controller controller, string throw new TempDataAssertionException(string.Format( "Expected TempData to have a non-null value with key \"{0}\", but none found.", key)); } + + if (value == null) return; + + if (actual.GetType() != value.GetType()) + { + throw new TempDataAssertionException(string.Format( + "Expected value to be of type {0}, but instead was {1}.", + value.GetType().FullName, + controller.TempData[key].GetType().FullName)); + } + + if (!value.Equals(actual)) + { + throw new TempDataAssertionException(string.Format( + "Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual)); + } } } } From da5cca21ab6026abe396c02855ba0d7198d51af0 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 15:39:07 +0100 Subject: [PATCH 03/10] Support for checking for temp data value with predicate. --- .../ControllerExtensionsTests.cs | 23 ++++++++++++++++++ .../ControllerExtensions.cs | 24 +++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs index 2a21e7d..c3e8721 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs @@ -111,5 +111,28 @@ public void Check_for_existent_temp_data_property_and_check_value_valid_using_re _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.")); + } } } diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index ff33fdb..80abe2a 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -72,21 +72,35 @@ public static void ShouldHaveTempDataProperty(this Controller controller, string "Expected TempData to have a non-null value with key \"{0}\", but none found.", key)); } - if (value == null) return; - - if (actual.GetType() != value.GetType()) + 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, controller.TempData[key].GetType().FullName)); - } + } - if (!value.Equals(actual)) + 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)); } } + + public static void ShouldHaveTempDataProperty(this Controller 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."); + } + } } } From ebd29371f4d2457b2c58a6d629ac1637137db8b9 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 16:02:07 +0100 Subject: [PATCH 04/10] Changed argument from type Controller to ControllerBase. ... In an attempt to adhere to Postel's law. --- TestStack.FluentMvcTesting/ControllerExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index 80abe2a..08d1d90 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -62,7 +62,7 @@ public static ControllerResultTest WithCallToChild(this T control return controller.WithCallTo(actionCall); } - public static void ShouldHaveTempDataProperty(this Controller controller, string key, object value = null) + public static void ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null) { var actual = controller.TempData[key]; @@ -87,7 +87,7 @@ public static void ShouldHaveTempDataProperty(this Controller controller, string } } - public static void ShouldHaveTempDataProperty(this Controller controller, string key, Func predicate) + public static void ShouldHaveTempDataProperty(this ControllerBase controller, string key, Func predicate) { var actual = controller.TempData[key]; From 1eb05aa62a7c9195e0799dc828d1a69969982331 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:02:43 +0100 Subject: [PATCH 05/10] Support for chaining temp data test methods. --- .../TestStack.FluentMVCTesting.Mvc3.csproj | 3 + .../TempDataResultTest.cs | 111 ++++++++++++++++++ .../TestStack.FluentMVCTesting.Tests.csproj | 3 +- .../ControllerExtensions.cs | 8 +- .../TempDataResultTest.cs | 56 +++++++++ .../TestStack.FluentMVCTesting.csproj | 1 + 6 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs create mode 100644 TestStack.FluentMvcTesting/TempDataResultTest.cs 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/TempDataResultTest.cs b/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs new file mode 100644 index 0000000..5e76a23 --- /dev/null +++ b/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs @@ -0,0 +1,111 @@ +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.")); + } + } +} \ 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 08d1d90..94a0bfa 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -62,7 +62,7 @@ public static ControllerResultTest WithCallToChild(this T control return controller.WithCallTo(actionCall); } - public static void ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null) + public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null) { var actual = controller.TempData[key]; @@ -85,9 +85,11 @@ public static void ShouldHaveTempDataProperty(this ControllerBase controller, st 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 void ShouldHaveTempDataProperty(this ControllerBase controller, string key, Func predicate) + public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, Func predicate) { var actual = controller.TempData[key]; @@ -101,6 +103,8 @@ public static void ShouldHaveTempDataProperty(this ControllerBase contro { throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed."); } + + return new TempDataResultTest(controller); } } } diff --git a/TestStack.FluentMvcTesting/TempDataResultTest.cs b/TestStack.FluentMvcTesting/TempDataResultTest.cs new file mode 100644 index 0000000..0c3940b --- /dev/null +++ b/TestStack.FluentMvcTesting/TempDataResultTest.cs @@ -0,0 +1,56 @@ +using System; +using System.Web.Mvc; + +namespace TestStack.FluentMVCTesting +{ + public class TempDataResultTest + { + private readonly ControllerBase _controller; + + public TempDataResultTest(ControllerBase controller) + { + _controller = controller; + } + + public void AndShouldHaveTempDataProperty(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, + _controller.TempData[key].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)); + } + } + + public void AndShouldHaveTempDataProperty(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."); + } + } + } +} \ 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 @@ + From 7df9316ceff009c4eeb93ae01247fb700b65fb9c Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:06:40 +0100 Subject: [PATCH 06/10] Improved test coverage. --- .../ControllerExtensionsTests.cs | 12 ++++++++++++ .../TempDataResultTest.cs | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs index c3e8721..21cd3b8 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs @@ -134,5 +134,17 @@ public void Check_for_existent_temp_data_property_and_check_value_using_invalid_ 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 index 5e76a23..d97f0f9 100644 --- a/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs +++ b/TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs @@ -107,5 +107,17 @@ public void Check_for_existent_temp_data_property_and_check_value_using_invalid_ 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 From 1feead85f845a46bf4944c97954448943f9adcc3 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:08:39 +0100 Subject: [PATCH 07/10] Readability tweak. --- TestStack.FluentMvcTesting/ControllerExtensions.cs | 2 +- TestStack.FluentMvcTesting/TempDataResultTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index 94a0bfa..0fbcc1e 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -77,7 +77,7 @@ public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase throw new TempDataAssertionException(string.Format( "Expected value to be of type {0}, but instead was {1}.", value.GetType().FullName, - controller.TempData[key].GetType().FullName)); + actual.GetType().FullName)); } if (value != null && !value.Equals(actual)) diff --git a/TestStack.FluentMvcTesting/TempDataResultTest.cs b/TestStack.FluentMvcTesting/TempDataResultTest.cs index 0c3940b..4057c1d 100644 --- a/TestStack.FluentMvcTesting/TempDataResultTest.cs +++ b/TestStack.FluentMvcTesting/TempDataResultTest.cs @@ -27,7 +27,7 @@ public void AndShouldHaveTempDataProperty(string key, object value = null) throw new TempDataAssertionException(string.Format( "Expected value to be of type {0}, but instead was {1}.", value.GetType().FullName, - _controller.TempData[key].GetType().FullName)); + actual.GetType().FullName)); } if (value != null && !value.Equals(actual)) From 3e440d8e64e98e54dc51cc2bfce3c065c2a6d586 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:17:59 +0100 Subject: [PATCH 08/10] Better support for chaining temp data test methods. --- TestStack.FluentMvcTesting/TempDataResultTest.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/TestStack.FluentMvcTesting/TempDataResultTest.cs b/TestStack.FluentMvcTesting/TempDataResultTest.cs index 4057c1d..64d0c42 100644 --- a/TestStack.FluentMvcTesting/TempDataResultTest.cs +++ b/TestStack.FluentMvcTesting/TempDataResultTest.cs @@ -12,7 +12,7 @@ public TempDataResultTest(ControllerBase controller) _controller = controller; } - public void AndShouldHaveTempDataProperty(string key, object value = null) + public TempDataResultTest AndShouldHaveTempDataProperty(string key, object value = null) { var actual = _controller.TempData[key]; @@ -35,9 +35,11 @@ public void AndShouldHaveTempDataProperty(string key, object value = null) throw new TempDataAssertionException(string.Format( "Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual)); } + + return this; } - public void AndShouldHaveTempDataProperty(string key, Func predicate) + public TempDataResultTest AndShouldHaveTempDataProperty(string key, Func predicate) { var actual = _controller.TempData[key]; @@ -51,6 +53,8 @@ public void AndShouldHaveTempDataProperty(string key, Func { throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed."); } + + return this; } } } \ No newline at end of file From c70054263efdb76d2b873399500db4190296c7b2 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:22:28 +0100 Subject: [PATCH 09/10] Made implementation adhere to DRY. --- .../TempDataResultTest.cs | 37 +------------------ 1 file changed, 2 insertions(+), 35 deletions(-) diff --git a/TestStack.FluentMvcTesting/TempDataResultTest.cs b/TestStack.FluentMvcTesting/TempDataResultTest.cs index 64d0c42..013378b 100644 --- a/TestStack.FluentMvcTesting/TempDataResultTest.cs +++ b/TestStack.FluentMvcTesting/TempDataResultTest.cs @@ -14,46 +14,13 @@ public TempDataResultTest(ControllerBase controller) public TempDataResultTest AndShouldHaveTempDataProperty(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)); - } - + _controller.ShouldHaveTempDataProperty(key, value); return this; } public TempDataResultTest AndShouldHaveTempDataProperty(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."); - } - + _controller.ShouldHaveTempDataProperty(key, predicate); return this; } } From 46af7b2592604cc98babca702f5bdec7f7b19904 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Wed, 10 Sep 2014 17:33:57 +0100 Subject: [PATCH 10/10] Removed ineffectual using directives. --- TestStack.FluentMvcTesting/ControllerExtensions.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TestStack.FluentMvcTesting/ControllerExtensions.cs b/TestStack.FluentMvcTesting/ControllerExtensions.cs index 0fbcc1e..efad62b 100644 --- a/TestStack.FluentMvcTesting/ControllerExtensions.cs +++ b/TestStack.FluentMvcTesting/ControllerExtensions.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq.Expressions; -using System.Reflection.Emit; using System.Threading.Tasks; using System.Web.Mvc;