Skip to content

Commit 3dcdc2d

Browse files
committed
Support for checking for temp data value.
1 parent b74b1e6 commit 3dcdc2d

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System.IO;
22
using NUnit.Framework;
33
using TestStack.FluentMVCTesting.Tests.TestControllers;
44

@@ -56,11 +56,60 @@ public void Check_for_existent_temp_data_property()
5656
public void Check_for_non_existent_temp_data_property()
5757
{
5858
const string key = "";
59+
5960
var exception = Assert.Throws<TempDataAssertionException>(() =>
6061
_controller.ShouldHaveTempDataProperty(key));
6162

6263
Assert.That(exception.Message, Is.EqualTo(string.Format(
6364
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
6465
}
66+
67+
[Test]
68+
public void Check_for_existent_temp_data_property_and_check_value()
69+
{
70+
const string key = "";
71+
const int value = 10;
72+
_controller.TempData[key] = value;
73+
74+
_controller.ShouldHaveTempDataProperty(key, value);
75+
}
76+
77+
[Test]
78+
public void Check_for_existent_temp_data_property_and_check_invalid_value()
79+
{
80+
const string key = "";
81+
const int actualValue = 0;
82+
const int expectedValue = 1;
83+
_controller.TempData[key] = actualValue;
84+
85+
var exception = Assert.Throws<TempDataAssertionException>(() =>
86+
_controller.ShouldHaveTempDataProperty(key, expectedValue));
87+
88+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue)));
89+
}
90+
91+
[Test]
92+
public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types()
93+
{
94+
const string key = "";
95+
const int actualValue = 0;
96+
const string expectedValue = "one";
97+
_controller.TempData[key] = actualValue;
98+
99+
var exception = Assert.Throws<TempDataAssertionException>(() =>
100+
_controller.ShouldHaveTempDataProperty(key, expectedValue));
101+
102+
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)));
103+
}
104+
105+
[Test]
106+
public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality()
107+
{
108+
const string key = "";
109+
MemoryStream expectedValue = new MemoryStream();
110+
_controller.TempData[key] = expectedValue;
111+
112+
_controller.ShouldHaveTempDataProperty(key, expectedValue);
113+
}
65114
}
66115
}

TestStack.FluentMvcTesting/ControllerExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
4+
using System.Reflection.Emit;
35
using System.Threading.Tasks;
46
using System.Web.Mvc;
57

@@ -60,7 +62,7 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
6062
return controller.WithCallTo(actionCall);
6163
}
6264

63-
public static void ShouldHaveTempDataProperty(this Controller controller, string key)
65+
public static void ShouldHaveTempDataProperty(this Controller controller, string key, object value = null)
6466
{
6567
var actual = controller.TempData[key];
6668

@@ -69,6 +71,22 @@ public static void ShouldHaveTempDataProperty(this Controller controller, string
6971
throw new TempDataAssertionException(string.Format(
7072
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
7173
}
74+
75+
if (value == null) return;
76+
77+
if (actual.GetType() != value.GetType())
78+
{
79+
throw new TempDataAssertionException(string.Format(
80+
"Expected value to be of type {0}, but instead was {1}.",
81+
value.GetType().FullName,
82+
controller.TempData[key].GetType().FullName));
83+
}
84+
85+
if (!value.Equals(actual))
86+
{
87+
throw new TempDataAssertionException(string.Format(
88+
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
89+
}
7290
}
7391
}
7492
}

0 commit comments

Comments
 (0)