Skip to content

Commit 1eb05aa

Browse files
committed
Support for chaining temp data test methods.
1 parent ebd2937 commit 1eb05aa

File tree

6 files changed

+179
-3
lines changed

6 files changed

+179
-3
lines changed

TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
<Compile Include="..\TestStack.FluentMvcTesting\ModelTest.cs">
8989
<Link>ModelTest.cs</Link>
9090
</Compile>
91+
<Compile Include="..\TestStack.FluentMvcTesting\TempDataResultTest.cs">
92+
<Link>TempDataResultTest.cs</Link>
93+
</Compile>
9194
<Compile Include="..\TestStack.FluentMvcTesting\ViewResultTest.cs">
9295
<Link>ViewResultTest.cs</Link>
9396
</Compile>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
using TestStack.FluentMVCTesting.Tests.TestControllers;
4+
5+
namespace TestStack.FluentMVCTesting.Tests
6+
{
7+
[TestFixture]
8+
public class TempDataResultTestShould
9+
{
10+
private ControllerExtensionsController _controller;
11+
private TempDataResultTest _tempDataTest;
12+
13+
[SetUp]
14+
public void Setup()
15+
{
16+
_controller = new ControllerExtensionsController();
17+
_tempDataTest = new TempDataResultTest(_controller);
18+
}
19+
20+
[Test]
21+
public void Check_for_existent_temp_data_property()
22+
{
23+
const string key = "";
24+
_controller.TempData[key] = "";
25+
26+
_tempDataTest.AndShouldHaveTempDataProperty(key);
27+
}
28+
29+
[Test]
30+
public void Check_for_non_existent_temp_data_property()
31+
{
32+
const string key = "";
33+
34+
var exception = Assert.Throws<TempDataAssertionException>(() =>
35+
_tempDataTest.AndShouldHaveTempDataProperty(key));
36+
37+
Assert.That(exception.Message, Is.EqualTo(string.Format(
38+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
39+
}
40+
41+
[Test]
42+
public void Check_for_existent_temp_data_property_and_check_value()
43+
{
44+
const string key = "";
45+
const int value = 10;
46+
_controller.TempData[key] = value;
47+
48+
_tempDataTest.AndShouldHaveTempDataProperty(key, value);
49+
}
50+
51+
[Test]
52+
public void Check_for_existent_temp_data_property_and_check_invalid_value()
53+
{
54+
const string key = "";
55+
const int actualValue = 0;
56+
const int expectedValue = 1;
57+
_controller.TempData[key] = actualValue;
58+
59+
var exception = Assert.Throws<TempDataAssertionException>(() =>
60+
_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue));
61+
62+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue)));
63+
}
64+
65+
[Test]
66+
public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types()
67+
{
68+
const string key = "";
69+
const int actualValue = 0;
70+
const string expectedValue = "one";
71+
_controller.TempData[key] = actualValue;
72+
73+
var exception = Assert.Throws<TempDataAssertionException>(() =>
74+
_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue));
75+
76+
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)));
77+
}
78+
79+
[Test]
80+
public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality()
81+
{
82+
const string key = "";
83+
MemoryStream expectedValue = new MemoryStream();
84+
_controller.TempData[key] = expectedValue;
85+
86+
_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue);
87+
}
88+
89+
[Test]
90+
public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate()
91+
{
92+
const string key = "";
93+
const int value = 1;
94+
_controller.TempData[key] = value;
95+
96+
_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == value);
97+
}
98+
99+
[Test]
100+
public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate()
101+
{
102+
const string key = "";
103+
_controller.TempData[key] = 1;
104+
105+
var exception = Assert.Throws<TempDataAssertionException>(() =>
106+
_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == 0));
107+
108+
Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
109+
}
110+
}
111+
}

TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<ItemGroup>
8080
<Compile Include="AsyncControllerTests.cs" />
8181
<Compile Include="RouteValueDictionaryExtensionsTests.cs" />
82+
<Compile Include="TempDataResultTest.cs" />
8283
<Compile Include="TestControllers\AsyncController.cs" />
8384
<Compile Include="ControllerExtensionsTests.cs" />
8485
<Compile Include="ControllerResultTestTests.cs" />
@@ -106,4 +107,4 @@
106107
<Target Name="AfterBuild">
107108
</Target>
108109
-->
109-
</Project>
110+
</Project>

TestStack.FluentMvcTesting/ControllerExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
6262
return controller.WithCallTo(actionCall);
6363
}
6464

65-
public static void ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null)
65+
public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null)
6666
{
6767
var actual = controller.TempData[key];
6868

@@ -85,9 +85,11 @@ public static void ShouldHaveTempDataProperty(this ControllerBase controller, st
8585
throw new TempDataAssertionException(string.Format(
8686
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
8787
}
88+
89+
return new TempDataResultTest(controller);
8890
}
8991

90-
public static void ShouldHaveTempDataProperty<TValue>(this ControllerBase controller, string key, Func<TValue, bool> predicate)
92+
public static TempDataResultTest ShouldHaveTempDataProperty<TValue>(this ControllerBase controller, string key, Func<TValue, bool> predicate)
9193
{
9294
var actual = controller.TempData[key];
9395

@@ -101,6 +103,8 @@ public static void ShouldHaveTempDataProperty<TValue>(this ControllerBase contro
101103
{
102104
throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed.");
103105
}
106+
107+
return new TempDataResultTest(controller);
104108
}
105109
}
106110
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Web.Mvc;
3+
4+
namespace TestStack.FluentMVCTesting
5+
{
6+
public class TempDataResultTest
7+
{
8+
private readonly ControllerBase _controller;
9+
10+
public TempDataResultTest(ControllerBase controller)
11+
{
12+
_controller = controller;
13+
}
14+
15+
public void AndShouldHaveTempDataProperty(string key, object value = null)
16+
{
17+
var actual = _controller.TempData[key];
18+
19+
if (actual == null)
20+
{
21+
throw new TempDataAssertionException(string.Format(
22+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
23+
}
24+
25+
if (value != null && actual.GetType() != value.GetType())
26+
{
27+
throw new TempDataAssertionException(string.Format(
28+
"Expected value to be of type {0}, but instead was {1}.",
29+
value.GetType().FullName,
30+
_controller.TempData[key].GetType().FullName));
31+
}
32+
33+
if (value != null && !value.Equals(actual))
34+
{
35+
throw new TempDataAssertionException(string.Format(
36+
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
37+
}
38+
}
39+
40+
public void AndShouldHaveTempDataProperty<TValue>(string key, Func<TValue, bool> predicate)
41+
{
42+
var actual = _controller.TempData[key];
43+
44+
if (actual == null)
45+
{
46+
throw new TempDataAssertionException(string.Format(
47+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
48+
}
49+
50+
if (!predicate((TValue)actual))
51+
{
52+
throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed.");
53+
}
54+
}
55+
}
56+
}

TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="ModelErrorTest.cs" />
8181
<Compile Include="ModelTest.cs" />
8282
<Compile Include="Properties\AssemblyInfo.cs" />
83+
<Compile Include="TempDataResultTest.cs" />
8384
<Compile Include="ViewResultTest.cs" />
8485
</ItemGroup>
8586
<ItemGroup>

0 commit comments

Comments
 (0)