Skip to content

Commit da5cca2

Browse files
committed
Support for checking for temp data value with predicate.
1 parent 3dcdc2d commit da5cca2

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,28 @@ public void Check_for_existent_temp_data_property_and_check_value_valid_using_re
111111

112112
_controller.ShouldHaveTempDataProperty(key, expectedValue);
113113
}
114+
115+
[Test]
116+
public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate()
117+
{
118+
const string key = "";
119+
const int value = 1;
120+
_controller.TempData[key] = value;
121+
122+
_controller
123+
.ShouldHaveTempDataProperty<int>(key, x => x == value);
124+
}
125+
126+
[Test]
127+
public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate()
128+
{
129+
const string key = "";
130+
_controller.TempData[key] = 1;
131+
132+
var exception = Assert.Throws<TempDataAssertionException>(() =>
133+
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));
134+
135+
Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
136+
}
114137
}
115138
}

TestStack.FluentMvcTesting/ControllerExtensions.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,35 @@ public static void ShouldHaveTempDataProperty(this Controller controller, string
7272
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
7373
}
7474

75-
if (value == null) return;
76-
77-
if (actual.GetType() != value.GetType())
75+
if (value != null && actual.GetType() != value.GetType())
7876
{
7977
throw new TempDataAssertionException(string.Format(
8078
"Expected value to be of type {0}, but instead was {1}.",
8179
value.GetType().FullName,
8280
controller.TempData[key].GetType().FullName));
83-
}
81+
}
8482

85-
if (!value.Equals(actual))
83+
if (value != null && !value.Equals(actual))
8684
{
8785
throw new TempDataAssertionException(string.Format(
8886
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
8987
}
9088
}
89+
90+
public static void ShouldHaveTempDataProperty<TValue>(this Controller controller, string key, Func<TValue, bool> predicate)
91+
{
92+
var actual = controller.TempData[key];
93+
94+
if (actual == null)
95+
{
96+
throw new TempDataAssertionException(string.Format(
97+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
98+
}
99+
100+
if (!predicate((TValue)actual))
101+
{
102+
throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed.");
103+
}
104+
}
91105
}
92106
}

0 commit comments

Comments
 (0)