Skip to content

Commit ed98e0e

Browse files
committed
Merge pull request #38 from ByteBlast/master
Refactored ControllerResultTest class.
2 parents ae832b8 + fed01a3 commit ed98e0e

25 files changed

+1377
-1299
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ _ReSharper*
1313
*/NUnit*/tools
1414
*/NhibernateProfiler*/tools
1515
packages
16-
*.DotSettings
1716
/TestStack.FluentMVCTesting.Example/App_Data

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 0 additions & 857 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text;
6+
using System.Web.Mvc;
7+
using TestStack.FluentMVCTesting.Tests.TestControllers;
8+
9+
namespace TestStack.FluentMVCTesting.Tests
10+
{
11+
[TestFixture]
12+
partial class ControllerResultTestShould
13+
{
14+
private ControllerResultTestController _controller;
15+
16+
[SetUp]
17+
public void Setup()
18+
{
19+
_controller = new ControllerResultTestController();
20+
}
21+
22+
#region General Tests
23+
// Expected action return types for the different types of assertions
24+
private static readonly List<Tuple<string, TestAction>> ReturnTypes = new List<Tuple<string, TestAction>>
25+
{
26+
ReturnType<EmptyResult>(t => t.ShouldReturnEmptyResult()),
27+
ReturnType<RedirectResult>(t => t.ShouldRedirectTo("")),
28+
ReturnType<RedirectToRouteResult>(t => t.ShouldRedirectTo(c => c.EmptyResult)),
29+
ReturnType<RedirectToRouteResult>(t => t.ShouldRedirectTo<SomeOtherController>(c => c.SomeAction())),
30+
ReturnType<ViewResult>(t => t.ShouldRenderView("")),
31+
ReturnType<ViewResult>(t => t.ShouldRenderDefaultView()),
32+
ReturnType<PartialViewResult>(t => t.ShouldRenderPartialView("")),
33+
ReturnType<PartialViewResult>(t => t.ShouldRenderDefaultPartialView()),
34+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
35+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
36+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
37+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("")),
38+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "")),
39+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)),
40+
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
41+
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
42+
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("", "")),
43+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
44+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream())),
45+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(contentType: "")),
46+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream(), "")),
47+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new byte[0])),
48+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new byte[0], "")),
49+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
50+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("", "")),
51+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("", "", Encoding.UTF8)),
52+
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
53+
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
54+
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
55+
ReturnType<ContentResult>(t => t.ShouldReturnContent()),
56+
ReturnType<ContentResult>(t => t.ShouldReturnContent("")),
57+
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "")),
58+
ReturnType<ContentResult>(t => t.ShouldReturnContent("", "", Encoding.UTF8))
59+
};
60+
61+
[Test]
62+
[TestCaseSource("ReturnTypes")]
63+
public void Check_return_type(Tuple<string, TestAction> test)
64+
{
65+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
66+
test.Item2(_controller.WithCallTo(c => c.RandomResult()))
67+
);
68+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected action result to be a {0}, but instead received a RandomResult.", test.Item1)));
69+
}
70+
71+
[Test]
72+
[TestCaseSource("ReturnTypes")]
73+
public void Check_null_return(Tuple<string, TestAction> test)
74+
{
75+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
76+
test.Item2(_controller.WithCallTo(c => c.Null()))
77+
);
78+
Assert.That(exception.Message, Is.EqualTo(string.Format("Received null action result when expecting {0}.", test.Item1)));
79+
}
80+
#endregion
81+
}
82+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Net;
2+
using NUnit.Framework;
3+
using TestStack.FluentMVCTesting.Tests.TestControllers;
4+
5+
namespace TestStack.FluentMVCTesting.Tests
6+
{
7+
partial class ControllerResultTestShould
8+
{
9+
[Test]
10+
public void Check_for_http_not_found()
11+
{
12+
_controller.WithCallTo(c => c.NotFound()).ShouldGiveHttpStatus(HttpStatusCode.NotFound);
13+
}
14+
15+
[Test]
16+
public void Check_for_http_status()
17+
{
18+
_controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code);
19+
}
20+
21+
[Test]
22+
public void Check_for_invalid_http_status()
23+
{
24+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
25+
_controller.WithCallTo(c => c.StatusCode()).ShouldGiveHttpStatus(ControllerResultTestController.Code + 1)
26+
);
27+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", ControllerResultTestController.Code + 1, ControllerResultTestController.Code)));
28+
}
29+
}
30+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Web.Mvc;
5+
using NUnit.Framework;
6+
using TestStack.FluentMVCTesting.Tests.TestControllers;
7+
8+
namespace TestStack.FluentMVCTesting.Tests
9+
{
10+
partial class ControllerResultTestShould
11+
{
12+
// Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion
13+
private static readonly List<RedirectToActionTestMetadata> ActionRedirects = new List<RedirectToActionTestMetadata>
14+
{
15+
ActionRedirect("ActionWithNoParameters",
16+
t => t.ShouldRedirectTo(c => c.ActionWithNoParameters),
17+
c => c.RedirectToActionWithNoParameters()
18+
),
19+
ActionRedirect("ActionWithOneParameter",
20+
t => t.ShouldRedirectTo(c => c.ActionWithOneParameter),
21+
c => c.RedirectToActionWithOneParameter()
22+
),
23+
ActionRedirect("ActionWithOneParameter",
24+
t => t.ShouldRedirectTo<int>(c => c.ActionWithOneParameter),
25+
c => c.RedirectToActionWithOneParameter()
26+
),
27+
ActionRedirect("ActionWithTwoParameters",
28+
t => t.ShouldRedirectTo<int, int>(c => c.ActionWithTwoParameters),
29+
c => c.RedirectToActionWithTwoParameters()
30+
),
31+
ActionRedirect("ActionWithThreeParameters",
32+
t => t.ShouldRedirectTo<int, int, int>(c => c.ActionWithThreeParameters),
33+
c => c.RedirectToActionWithThreeParameters()
34+
),
35+
ActionRedirect("ActionWithNoParameters",
36+
t => t.ShouldRedirectTo(c => c.ActionWithNoParameters()),
37+
c => c.RedirectToActionWithNoParameters()
38+
),
39+
ActionRedirect("ActionWithOneParameter",
40+
t => t.ShouldRedirectTo(c => c.ActionWithOneParameter(0)),
41+
c => c.RedirectToActionWithOneParameter()
42+
),
43+
ActionRedirect("ActionWithTwoParameters",
44+
t => t.ShouldRedirectTo(c => c.ActionWithTwoParameters(0, 0)),
45+
c => c.RedirectToActionWithTwoParameters()
46+
),
47+
ActionRedirect("ActionWithThreeParameters",
48+
t => t.ShouldRedirectTo(c => c.ActionWithThreeParameters(0, 0, 0)),
49+
c => c.RedirectToActionWithThreeParameters()
50+
),
51+
ActionRedirect("ActionWithMoreThanThreeParameters",
52+
t => t.ShouldRedirectTo(c => c.ActionWithMoreThanThreeParameters(0, 0, 0, 0)),
53+
c => c.RedirectToActionWithMoreThanThreeParameters()
54+
),
55+
ActionRedirect("ActionWithMoreThanThreeParameters",
56+
t => t.ShouldRedirectTo(typeof(ControllerResultTestController).GetMethod("ActionWithMoreThanThreeParameters")),
57+
c => c.RedirectToActionWithMoreThanThreeParameters()
58+
),
59+
};
60+
61+
// Different ways that redirects to another controller can be asserted
62+
private static readonly List<TestAction> OtherControllerRedirects = new List<TestAction>
63+
{
64+
c => c.ShouldRedirectTo<SomeOtherController>(typeof(SomeOtherController).GetMethod("SomeAction")),
65+
c => c.ShouldRedirectTo<SomeOtherController>(c2 => c2.SomeAction()),
66+
};
67+
68+
public class RedirectToActionTestMetadata : Tuple<string, TestAction, Expression<Func<ControllerResultTestController, ActionResult>>>
69+
{
70+
public RedirectToActionTestMetadata(string expectedMethodName, TestAction testCall, Expression<Func<ControllerResultTestController, ActionResult>> validControllerActionCall)
71+
: base(expectedMethodName, testCall, validControllerActionCall) { }
72+
}
73+
74+
public delegate void TestAction(ControllerResultTest<ControllerResultTestController> testClass);
75+
76+
private static Tuple<string, TestAction> ReturnType<T>(TestAction a)
77+
{
78+
return new Tuple<string, TestAction>(typeof(T).Name, a);
79+
}
80+
81+
private static RedirectToActionTestMetadata ActionRedirect(string s, TestAction a, Expression<Func<ControllerResultTestController, ActionResult>> c)
82+
{
83+
return new RedirectToActionTestMetadata(s, a, c);
84+
}
85+
86+
[Test]
87+
public void Check_for_redirect_to_url()
88+
{
89+
_controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(ControllerResultTestController.RedirectUrl);
90+
}
91+
92+
[Test]
93+
public void Check_for_redirect_to_invalid_url()
94+
{
95+
const string url = "http://validurl/";
96+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
97+
_controller.WithCallTo(c => c.RedirectToUrl()).ShouldRedirectTo(url)
98+
);
99+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, ControllerResultTestController.RedirectUrl)));
100+
}
101+
102+
[Test]
103+
public void Check_for_redirect_to_route_name()
104+
{
105+
_controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(ControllerResultTestController.RouteName);
106+
}
107+
108+
[Test]
109+
public void Check_for_redirect_to_invalid_route_name()
110+
{
111+
const string routeName = "ValidRoute";
112+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
113+
_controller.WithCallTo(c => c.RedirectToRouteName()).ShouldRedirectToRoute(routeName)
114+
);
115+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", routeName, ControllerResultTestController.RouteName)));
116+
}
117+
118+
[Test]
119+
[TestCaseSource("ActionRedirects")]
120+
public void Check_for_redirect_to_action(RedirectToActionTestMetadata test)
121+
{
122+
test.Item2(_controller.WithCallTo(test.Item3));
123+
}
124+
125+
[Test]
126+
[TestCaseSource("ActionRedirects")]
127+
public void Check_for_redirect_to_incorrect_controller(RedirectToActionTestMetadata test)
128+
{
129+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
130+
test.Item2(_controller.WithCallTo(c => c.RedirectToAnotherController()))
131+
);
132+
Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'ControllerResultTest', but instead was given a redirect to controller 'SomeOther'."));
133+
}
134+
135+
[Test]
136+
[TestCaseSource("ActionRedirects")]
137+
public void Check_for_redirect_to_incorrect_action(RedirectToActionTestMetadata test)
138+
{
139+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
140+
test.Item2(_controller.WithCallTo(c => c.RedirectToRandomResult()))
141+
);
142+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action 'RandomResult'.", test.Item1)));
143+
}
144+
145+
// todo: Test the route values expectations
146+
147+
[Test]
148+
[TestCaseSource("ActionRedirects")]
149+
public void Check_for_redirect_to_empty_action(RedirectToActionTestMetadata test)
150+
{
151+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
152+
test.Item2(_controller.WithCallTo(c => c.RedirectToRouteName()))
153+
);
154+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", test.Item1)));
155+
}
156+
157+
[Test]
158+
[TestCaseSource("OtherControllerRedirects")]
159+
public void Check_for_redirect_to_another_controller(TestAction action)
160+
{
161+
action(_controller.WithCallTo(c => c.RedirectToAnotherController()));
162+
}
163+
164+
[Test]
165+
public void Check_for_redirect_to_incorrect_other_controller()
166+
{
167+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
168+
_controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo<YetAnotherController>(c => c.SomeAction())
169+
);
170+
Assert.That(exception.Message, Is.EqualTo("Expected redirect to controller 'YetAnother', but instead was given a redirect to controller 'SomeOther'."));
171+
}
172+
173+
[Test]
174+
public void Check_for_redirect_to_incorrect_action_in_another_controller()
175+
{
176+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
177+
_controller.WithCallTo(c => c.RedirectToAnotherController()).ShouldRedirectTo<SomeOtherController>(c => c.SomeOtherAction())
178+
);
179+
Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction', but instead was given a redirect to action 'SomeAction'."));
180+
}
181+
182+
[Test]
183+
public void Check_for_redirect_to_action_with_non_specified_controller()
184+
{
185+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
186+
_controller.WithCallTo(c => c.RedirectToAnotherActionNoController()).ShouldRedirectTo<SomeOtherController>(c => c.SomeOtherAction())
187+
);
188+
Assert.That(exception.Message, Is.EqualTo("Expected redirect to action 'SomeOtherAction' in 'SomeOther' controller, but instead was given redirect to action 'SomeAction' within the same controller."));
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)