Skip to content

Commit b74b1e6

Browse files
committed
Support for checking for temp data key existence.
1 parent 6d52160 commit b74b1e6

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
23
using TestStack.FluentMVCTesting.Tests.TestControllers;
34

45
namespace TestStack.FluentMVCTesting.Tests
@@ -41,5 +42,25 @@ public void Throw_exception_for_child_action_call_to_non_child_action()
4142
var exception = Assert.Throws<InvalidControllerActionException>(() => _controller.WithCallToChild(c => c.SomeAction()));
4243
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."));
4344
}
45+
46+
[Test]
47+
public void Check_for_existent_temp_data_property()
48+
{
49+
const string key = "";
50+
_controller.TempData[key] = "";
51+
52+
_controller.ShouldHaveTempDataProperty(key);
53+
}
54+
55+
[Test]
56+
public void Check_for_non_existent_temp_data_property()
57+
{
58+
const string key = "";
59+
var exception = Assert.Throws<TempDataAssertionException>(() =>
60+
_controller.ShouldHaveTempDataProperty(key));
61+
62+
Assert.That(exception.Message, Is.EqualTo(string.Format(
63+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
64+
}
4465
}
4566
}

TestStack.FluentMvcTesting/ControllerExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,16 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
5959

6060
return controller.WithCallTo(actionCall);
6161
}
62+
63+
public static void ShouldHaveTempDataProperty(this Controller controller, string key)
64+
{
65+
var actual = controller.TempData[key];
66+
67+
if (actual == null)
68+
{
69+
throw new TempDataAssertionException(string.Format(
70+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
71+
}
72+
}
6273
}
6374
}

TestStack.FluentMvcTesting/Exceptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace TestStack.FluentMVCTesting
44
{
55

6+
public class TempDataAssertionException : Exception
7+
{
8+
public TempDataAssertionException(string message) : base(message) { }
9+
}
10+
611
public class ActionResultAssertionException : Exception
712
{
813
public ActionResultAssertionException(string message) : base(message) { }

0 commit comments

Comments
 (0)