Skip to content

Commit c55d859

Browse files
Implementation of extensions for fluent RouteValueDictionary assertions
1 parent 1bcaa86 commit c55d859

File tree

10 files changed

+209
-2
lines changed

10 files changed

+209
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using NUnit.Framework;
3+
using TestStack.FluentMVCTesting.Sample.Controllers;
4+
5+
namespace TestStack.FluentMVCTesting.Sample.Tests.Controllers
6+
{
7+
[TestFixture]
8+
class ProductControllerTests
9+
{
10+
private ProductController _controller;
11+
12+
[SetUp]
13+
public void SetUp()
14+
{
15+
_controller = new ProductController();
16+
}
17+
18+
[Test]
19+
public void WhenViewingIndexPage_ThenShouldRedirectToProduct_WithId()
20+
{
21+
_controller.WithCallTo(c => c.Index())
22+
.ShouldRedirectTo(c => c.Product())
23+
.WithRouteValue("Id");
24+
}
25+
26+
[Test]
27+
public void WhenViewingIndexPage_ThenShouldRedirectToProduct_WithIdEqualToOne()
28+
{
29+
_controller.WithCallTo(c => c.Index())
30+
.ShouldRedirectTo(c => c.Product())
31+
.WithRouteValue("Id", 1);
32+
}
33+
34+
[Test]
35+
public void UnknownUserAction_RedirectsToUser_WithEmptyGuidAndUnknownUserType()
36+
{
37+
_controller.WithCallTo(c => c.UnknownUser())
38+
.ShouldRedirectTo(c => c.User())
39+
.WithRouteValue("Id", Guid.Empty)
40+
.WithRouteValue("UserType", UserType.Unknown);
41+
}
42+
}
43+
}

Samples/TestStack.FluentMVCTesting.Sample.Tests/TestStack.FluentMVCTesting.Sample.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
</ItemGroup>
7878
<ItemGroup>
7979
<Compile Include="Controllers\AccountControllerTests.cs" />
80+
<Compile Include="Controllers\ProductControllerTests.cs" />
8081
<Compile Include="Properties\AssemblyInfo.cs" />
8182
</ItemGroup>
8283
<ItemGroup>
@@ -101,4 +102,4 @@
101102
<Target Name="AfterBuild">
102103
</Target>
103104
-->
104-
</Project>
105+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Web.Mvc;
3+
4+
namespace TestStack.FluentMVCTesting.Sample.Controllers
5+
{
6+
public enum UserType
7+
{
8+
Unknown = 0
9+
}
10+
11+
public class ProductController : Controller
12+
{
13+
public ActionResult Product()
14+
{
15+
return View();
16+
}
17+
18+
public ActionResult Index()
19+
{
20+
return RedirectToAction("Product", new { Id = 1 });
21+
}
22+
23+
public ActionResult User()
24+
{
25+
return View();
26+
}
27+
28+
public ActionResult UnknownUser()
29+
{
30+
return RedirectToAction("User", new { Id = Guid.Empty, UserType = UserType.Unknown });
31+
}
32+
}
33+
}

Samples/TestStack.FluentMVCTesting.Sample/TestStack.FluentMVCTesting.Sample.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="App_Start\RouteConfig.cs" />
174174
<Compile Include="Controllers\AccountController.cs" />
175175
<Compile Include="Controllers\HomeController.cs" />
176+
<Compile Include="Controllers\ProductController.cs" />
176177
<Compile Include="Filters\InitializeSimpleMembershipAttribute.cs" />
177178
<Compile Include="Global.asax.cs">
178179
<DependentUpon>Global.asax</DependentUpon>
@@ -292,6 +293,7 @@
292293
<Content Include="Views\Shared\_LoginPartial.cshtml" />
293294
<Content Include="Views\Shared\_Layout.cshtml" />
294295
<Content Include="Views\Web.config" />
296+
<Content Include="Views\Product\Product.cshtml" />
295297
</ItemGroup>
296298
<ItemGroup>
297299
<Folder Include="App_Data\" />
@@ -334,4 +336,4 @@
334336
</Target>
335337
<Target Name="AfterBuild">
336338
</Target> -->
337-
</Project>
339+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@model dynamic
2+
3+
@{
4+
ViewBag.Title = "title";
5+
Layout = "_Layout";
6+
}
7+
8+
<h2>title</h2>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Web.Routing;
2+
using NUnit.Framework;
3+
4+
namespace TestStack.FluentMVCTesting.Tests
5+
{
6+
[TestFixture]
7+
class RouteValueDictionaryExtensionsTests
8+
{
9+
[Test]
10+
public void WithRouteValue_ThrowsWhenDictIsEmpty()
11+
{
12+
var dict = new RouteValueDictionary();
13+
14+
Assert.Throws<MissingRouteValueException>(() => dict.WithRouteValue("NotImportant"));
15+
}
16+
17+
[Test]
18+
public void WithRouteValue_ThrowsWhenDictDoesntHaveValue()
19+
{
20+
var dict = new RouteValueDictionary();
21+
dict.Add("Existing", "NotImportant");
22+
23+
Assert.Throws<MissingRouteValueException>(() => dict.WithRouteValue("NotExisting"));
24+
}
25+
26+
[Test]
27+
public void WithRouteValue_ThrowsWhenDictHasIncorrectValue()
28+
{
29+
var dict = new RouteValueDictionary();
30+
dict.Add("Existing", "Correct");
31+
32+
Assert.Throws<InvalidRouteValueException>(() => dict.WithRouteValue("Existing", "Incorrect"));
33+
}
34+
35+
[Test]
36+
public void WithRouteValue_ThrowIfValueTypeMismatch()
37+
{
38+
var dict = new RouteValueDictionary();
39+
dict.Add("Existing", 1);
40+
41+
Assert.Throws<ValueTypeMismatchException>(() => dict.WithRouteValue("Existing", "1"));
42+
}
43+
44+
[Test]
45+
public void WithRouteValue_DoesntThrowIfValueIsCorrect()
46+
{
47+
var dict = new RouteValueDictionary();
48+
dict.Add("Existing", "Correct");
49+
50+
dict.WithRouteValue("Existing", "Correct");
51+
52+
Assert.Pass();
53+
}
54+
55+
[Test]
56+
public void WithRouteValue_CanUseIntType()
57+
{
58+
var dict = new RouteValueDictionary();
59+
dict.Add("Existing", 10);
60+
61+
dict.WithRouteValue("Existing", 10);
62+
63+
Assert.Pass();
64+
}
65+
}
66+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
</ItemGroup>
8080
<ItemGroup>
8181
<Compile Include="AsyncControllerTests.cs" />
82+
<Compile Include="RouteValueDictionaryExtensionsTests.cs" />
8283
<Compile Include="TestControllers\AsyncController.cs" />
8384
<Compile Include="ControllerExtensionsTests.cs" />
8485
<Compile Include="ControllerResultTestTests.cs" />

TestStack.FluentMvcTesting/Exceptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,19 @@ public class InvalidControllerActionException : Exception
2222
{
2323
public InvalidControllerActionException(string message) : base(message) { }
2424
}
25+
26+
public class InvalidRouteValueException : Exception
27+
{
28+
public InvalidRouteValueException(string message) : base(message) { }
29+
}
30+
31+
public class MissingRouteValueException : Exception
32+
{
33+
public MissingRouteValueException(string message) : base(message) { }
34+
}
35+
36+
public class ValueTypeMismatchException : Exception
37+
{
38+
public ValueTypeMismatchException(string message) : base(message) { }
39+
}
2540
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Web.Routing;
3+
4+
namespace TestStack.FluentMVCTesting
5+
{
6+
public static class RouteValueDictionaryExtension
7+
{
8+
public static RouteValueDictionary WithRouteValue(this RouteValueDictionary dict, string key)
9+
{
10+
if (!dict.ContainsKey(key))
11+
{
12+
throw new MissingRouteValueException(string.Format("No value {0} in the route dictionary", key));
13+
}
14+
15+
return dict;
16+
}
17+
18+
public static RouteValueDictionary WithRouteValue<T>(this RouteValueDictionary dict, string key, T value)
19+
{
20+
dict.WithRouteValue(key);
21+
22+
var actualValue = dict[key];
23+
24+
if (!(actualValue is T))
25+
{
26+
throw new ValueTypeMismatchException(string.Format("Invalid type of Value with key {0} \r\n expected {1} \r\n but got {2}", key, value.GetType(), actualValue.GetType()));
27+
}
28+
29+
if (!Equals(actualValue, value))
30+
{
31+
throw new InvalidRouteValueException(string.Format("Invalid value for key {0} \r\n expected {1} \r\n but got {2} in the route dictionary",key ,value, actualValue));
32+
}
33+
34+
return dict;
35+
}
36+
}
37+
}

TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Reference Include="System.Xml" />
7575
</ItemGroup>
7676
<ItemGroup>
77+
<Compile Include="RouteValueDictionaryExtension.cs" />
7778
<Compile Include="ControllerExtensions.cs" />
7879
<Compile Include="ControllerResultTest.cs" />
7980
<Compile Include="Exceptions.cs" />

0 commit comments

Comments
 (0)