Skip to content

Commit 4f991ac

Browse files
committed
Merge pull request #33 from ByteBlast/master
Support for checking for temp data values.
2 parents 6d52160 + 46af7b2 commit 4f991ac

File tree

8 files changed

+312
-2
lines changed

8 files changed

+312
-2
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>

TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs

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

45
namespace TestStack.FluentMVCTesting.Tests
@@ -41,5 +42,109 @@ 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+
60+
var exception = Assert.Throws<TempDataAssertionException>(() =>
61+
_controller.ShouldHaveTempDataProperty(key));
62+
63+
Assert.That(exception.Message, Is.EqualTo(string.Format(
64+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
65+
}
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+
}
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+
}
137+
138+
[Test]
139+
public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate()
140+
{
141+
const string key = "";
142+
143+
var exception = Assert.Throws<TempDataAssertionException>(() =>
144+
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));
145+
146+
Assert.That(exception.Message, Is.EqualTo(string.Format(
147+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
148+
}
44149
}
45150
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
[Test]
112+
public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate()
113+
{
114+
const string key = "";
115+
116+
var exception = Assert.Throws<TempDataAssertionException>(() =>
117+
_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == 0));
118+
119+
Assert.That(exception.Message, Is.EqualTo(string.Format(
120+
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
121+
}
122+
}
123+
}

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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,50 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
5959

6060
return controller.WithCallTo(actionCall);
6161
}
62+
63+
public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null)
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+
73+
if (value != null && actual.GetType() != value.GetType())
74+
{
75+
throw new TempDataAssertionException(string.Format(
76+
"Expected value to be of type {0}, but instead was {1}.",
77+
value.GetType().FullName,
78+
actual.GetType().FullName));
79+
}
80+
81+
if (value != null && !value.Equals(actual))
82+
{
83+
throw new TempDataAssertionException(string.Format(
84+
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
85+
}
86+
87+
return new TempDataResultTest(controller);
88+
}
89+
90+
public static TempDataResultTest ShouldHaveTempDataProperty<TValue>(this ControllerBase 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+
105+
return new TempDataResultTest(controller);
106+
}
62107
}
63108
}

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) { }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 TempDataResultTest AndShouldHaveTempDataProperty(string key, object value = null)
16+
{
17+
_controller.ShouldHaveTempDataProperty(key, value);
18+
return this;
19+
}
20+
21+
public TempDataResultTest AndShouldHaveTempDataProperty<TValue>(string key, Func<TValue, bool> predicate)
22+
{
23+
_controller.ShouldHaveTempDataProperty(key, predicate);
24+
return this;
25+
}
26+
}
27+
}

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)