Skip to content

Commit f8dc82a

Browse files
author
CaseyBurns
committed
Implemented url assertions and most ActionResult assertions. More unit tests
1 parent b45553e commit f8dc82a

10 files changed

+336
-134
lines changed

src/FluentAssertions.Mvc.Tests/ActionResultAssertions_Tests.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,101 @@
33
using FluentAssertions.Mvc3.Tests.Fakes;
44
using System.Web.Mvc;
55
using FluentAssertions.Mvc3;
6+
using System.Web.Routing;
67

78
namespace FluentAssertions.Mvc3.Tests
89
{
910
[TestFixture]
1011
public class ActionResultAssertions_Tests
1112
{
13+
[Test]
14+
public void BeContent_GivenContent_ShouldPass()
15+
{
16+
ActionResult result = new ContentResult();
17+
result.Should().BeContent();
18+
}
19+
20+
[Test]
21+
public void BeContent_GivenNotContent_ShouldFail()
22+
{
23+
ActionResult result = new ViewResult();
24+
Action a = () => result.Should().BeContent();
25+
a.ShouldThrow<Exception>();
26+
}
27+
28+
[Test]
29+
public void BeEmpty_GivenEmpty_ShouldPass()
30+
{
31+
ActionResult result = new EmptyResult();
32+
result.Should().BeEmpty();
33+
}
34+
35+
[Test]
36+
public void BeEmpty_GivenNotEmpty_ShouldPass()
37+
{
38+
ActionResult result = new ViewResult();
39+
Action a = () => result.Should().BeEmpty();
40+
a.ShouldThrow<Exception>();
41+
}
42+
43+
[Test]
44+
public void BeRedirectToRoute_GivenRedirectToRoute_ShouldPass()
45+
{
46+
ActionResult result = new RedirectToRouteResult(new RouteValueDictionary());
47+
result.Should().BeRedirectToRoute();
48+
}
49+
50+
[Test]
51+
public void BeRedirectToRoute_GivenNotRedirectToRoute_ShouldFail()
52+
{
53+
ActionResult result = new ViewResult();
54+
Action a = () => result.Should().BeRedirectToRoute();
55+
a.ShouldThrow<Exception>();
56+
}
57+
58+
[Test]
59+
public void BeRedirect_GivenRedirect_ShouldPass()
60+
{
61+
ActionResult result = new RedirectResult("/");
62+
result.Should().BeRedirect();
63+
}
64+
65+
[Test]
66+
public void BeRedirect_GivenNotRedirect_ShouldFail()
67+
{
68+
ActionResult result = new ViewResult();
69+
Action a = () => result.Should().BePartialView();
70+
a.ShouldThrow<Exception>();
71+
}
72+
73+
[Test]
74+
public void BePartialView_GivenPartial_ShouldPass()
75+
{
76+
ActionResult result = new PartialViewResult();
77+
result.Should().BePartialView();
78+
}
79+
80+
[Test]
81+
public void BePartialView_GivenNotPartial_ShouldFail()
82+
{
83+
ActionResult result = new RedirectResult("/");
84+
Action a = () => result.Should().BePartialView();
85+
a.ShouldThrow<Exception>();
86+
}
87+
88+
[Test]
89+
public void BeView_GivenView_ShouldPass()
90+
{
91+
ActionResult result = new ViewResult();
92+
result.Should().BeView();
93+
}
94+
95+
[Test]
96+
public void BeView_GivenNotView_ShouldFail()
97+
{
98+
ActionResult result = new RedirectResult("/");
99+
Action a = () => result.Should().BeView();
100+
a.ShouldThrow<Exception>();
101+
}
12102
}
13103
}

src/FluentAssertions.Mvc.Tests/FluentAssertions.Mvc3.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<SchemaVersion>2.0</SchemaVersion>
88
<ProjectGuid>{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}</ProjectGuid>
99
<OutputType>Library</OutputType>
10-
<RootNamespace>FluentAssertions.Mvc.Tests</RootNamespace>
11-
<AssemblyName>FluentAssertions.Mvc.Tests</AssemblyName>
10+
<RootNamespace>FluentAssertions.Mvc3.Tests</RootNamespace>
11+
<AssemblyName>FluentAssertions.Mvc3.Tests</AssemblyName>
1212
</PropertyGroup>
1313
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1414
<DebugSymbols>true</DebugSymbols>
@@ -51,6 +51,7 @@
5151
<Compile Include="Properties\AssemblyInfo.cs" />
5252
<Compile Include="Fakes\FakeController.cs" />
5353
<Compile Include="ActionResultAssertions_Tests.cs" />
54+
<Compile Include="RedirectResultAssertions_Tests.cs" />
5455
<Compile Include="RouteDataAssertions_Tests.cs" />
5556
<Compile Include="RouteValueDictionary_Extensions_Tests.cs" />
5657
<Compile Include="ViewResultAssertions_Tests.cs" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using System.Web.Mvc;
7+
using FluentAssertions.Mvc3;
8+
9+
namespace FluentAssertions.Mvc3.Tests
10+
{
11+
[TestFixture]
12+
public class RedirectResultAssertions_Tests
13+
{
14+
[Test]
15+
public void HaveUrl_GivenValidUrl_ShouldPass()
16+
{
17+
ActionResult result = new RedirectResult("/abc");
18+
19+
result.Should().BeRedirect()
20+
.HaveUrl("/abc");
21+
}
22+
}
23+
}

src/FluentAssertions.Mvc.Tests/RouteValueDictionary_Extensions_Tests.cs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace FluentAssertions.Mvc3.Tests
1111
[TestFixture]
1212
public class RouteValueDictionary_Extensions_Tests
1313
{
14-
public static void a()
14+
private RouteCollection CreateDefaultRoutes()
1515
{
1616
var routes = new RouteCollection();
1717
routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
@@ -23,15 +23,80 @@ public static void a()
2323
Id = UrlParameter.Optional
2424
}),
2525
});
26+
return routes;
27+
}
28+
29+
[Test]
30+
public void GenerateUrl_GivenNoRouteValues_ShouldReturnExpectedUrl()
31+
{
32+
var routeValues = new RouteValueDictionary();
33+
34+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
35+
url.Should().Be("/");
36+
}
37+
38+
[Test]
39+
public void GenerateUrl_GivenNotDefaultControllerValue_ShouldReturnExpectedUrl()
40+
{
41+
var routeValues = new RouteValueDictionary(new
42+
{
43+
Controller = "Blah"
44+
});
2645

46+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
47+
url.Should().Be("/Blah");
48+
}
49+
50+
[Test]
51+
public void GenerateUrl_GivenDefaultControllerValue_ShouldReturnExpectedUrl()
52+
{
53+
var routeValues = new RouteValueDictionary(new
54+
{
55+
Controller = "Home"
56+
});
57+
58+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
59+
url.Should().Be("/");
60+
}
61+
62+
[Test]
63+
public void GenerateUrl_GivenDefaultControllerAndActionValues_ShouldReturnExpectedUrl()
64+
{
2765
var routeValues = new RouteValueDictionary(new
2866
{
2967
Controller = "Home",
3068
Action = "Index"
3169
});
3270

33-
var url = routeValues.GenerateUrl(routes);
34-
url.Should().Be("/abcHome");
71+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
72+
url.Should().Be("/");
73+
}
74+
75+
[Test]
76+
public void GenerateUrl_GivenDefaultControllerAndNotDefaultActionValues_ShouldReturnExpectedUrl()
77+
{
78+
var routeValues = new RouteValueDictionary(new
79+
{
80+
Controller = "Home",
81+
Action = "action1"
82+
});
83+
84+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
85+
url.Should().Be("/Home/action1");
3586
}
87+
88+
[Test]
89+
public void GenerateUrl_GivenNotDefaultControllerAndActionValues_ShouldReturnExpectedUrl()
90+
{
91+
var routeValues = new RouteValueDictionary(new
92+
{
93+
Controller = "blah",
94+
Action = "action1"
95+
});
96+
97+
var url = routeValues.GenerateUrl(CreateDefaultRoutes());
98+
url.Should().Be("/blah/action1");
99+
}
100+
36101
}
37102
}

0 commit comments

Comments
 (0)