Skip to content

Commit 43086a6

Browse files
committed
Refactored ControllerResultTest.
1 parent 7924f9b commit 43086a6

File tree

10 files changed

+467
-437
lines changed

10 files changed

+467
-437
lines changed

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 0 additions & 436 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Web.Mvc;
2+
3+
namespace TestStack.FluentMVCTesting
4+
{
5+
public partial class ControllerResultTest<T> where T : Controller
6+
{
7+
private readonly T _controller;
8+
private readonly string _actionName;
9+
private readonly ActionResult _actionResult;
10+
11+
private void ValidateActionReturnType<TActionResult>() where TActionResult : ActionResult
12+
{
13+
var castedActionResult = _actionResult as TActionResult;
14+
15+
if (_actionResult == null)
16+
throw new ActionResultAssertionException(string.Format("Received null action result when expecting {0}.", typeof(TActionResult).Name));
17+
18+
if (castedActionResult == null)
19+
throw new ActionResultAssertionException(
20+
string.Format("Expected action result to be a {0}, but instead received a {1}.",
21+
typeof(TActionResult).Name, _actionResult.GetType().Name
22+
)
23+
);
24+
}
25+
26+
public ControllerResultTest(T controller, string actionName, ActionResult actionResult)
27+
{
28+
_controller = controller;
29+
_actionName = actionName;
30+
_actionResult = actionResult;
31+
}
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Net;
2+
using System.Web.Mvc;
3+
4+
namespace TestStack.FluentMVCTesting
5+
{
6+
public partial class ControllerResultTest<T>
7+
{
8+
#region Http Status
9+
10+
public void ShouldGiveHttpStatus()
11+
{
12+
ValidateActionReturnType<HttpStatusCodeResult>();
13+
}
14+
15+
public void ShouldGiveHttpStatus(int status)
16+
{
17+
ValidateActionReturnType<HttpStatusCodeResult>();
18+
19+
var statusCodeResult = (HttpStatusCodeResult)_actionResult;
20+
21+
if (statusCodeResult.StatusCode != status)
22+
throw new ActionResultAssertionException(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", status, statusCodeResult.StatusCode));
23+
}
24+
25+
public void ShouldGiveHttpStatus(HttpStatusCode status)
26+
{
27+
ShouldGiveHttpStatus((int)status);
28+
}
29+
30+
#endregion
31+
}
32+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using System.Reflection;
4+
using System.Text.RegularExpressions;
5+
using System.Web.Mvc;
6+
using System.Web.Routing;
7+
8+
namespace TestStack.FluentMVCTesting
9+
{
10+
public partial class ControllerResultTest<T>
11+
{
12+
public void ShouldRedirectTo(string url)
13+
{
14+
ValidateActionReturnType<RedirectResult>();
15+
var redirectResult = (RedirectResult)_actionResult;
16+
17+
if (redirectResult.Url != url)
18+
throw new ActionResultAssertionException(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, redirectResult.Url));
19+
}
20+
21+
public RouteValueDictionary ShouldRedirectToRoute(string route)
22+
{
23+
ValidateActionReturnType<RedirectToRouteResult>();
24+
var redirectResult = (RedirectToRouteResult)_actionResult;
25+
26+
if (redirectResult.RouteName != route)
27+
throw new ActionResultAssertionException(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", route, redirectResult.RouteName));
28+
29+
return redirectResult.RouteValues;
30+
}
31+
32+
public RouteValueDictionary ShouldRedirectTo(Func<T, Func<ActionResult>> actionRedirectedTo)
33+
{
34+
return ShouldRedirectTo(actionRedirectedTo(_controller).Method);
35+
}
36+
37+
public RouteValueDictionary ShouldRedirectTo(Func<T, Func<int, ActionResult>> actionRedirectedTo)
38+
{
39+
return ShouldRedirectTo(actionRedirectedTo(_controller).Method);
40+
}
41+
42+
public RouteValueDictionary ShouldRedirectTo<T1>(Func<T, Func<T1, ActionResult>> actionRedirectedTo)
43+
{
44+
return ShouldRedirectTo(actionRedirectedTo(_controller).Method);
45+
}
46+
47+
public RouteValueDictionary ShouldRedirectTo<T1, T2>(Func<T, Func<T1, T2, ActionResult>> actionRedirectedTo)
48+
{
49+
return ShouldRedirectTo(actionRedirectedTo(_controller).Method);
50+
}
51+
52+
public RouteValueDictionary ShouldRedirectTo<T1, T2, T3>(Func<T, Func<T1, T2, T3, ActionResult>> actionRedirectedTo)
53+
{
54+
return ShouldRedirectTo(actionRedirectedTo(_controller).Method);
55+
}
56+
57+
public RouteValueDictionary ShouldRedirectTo(Expression<Action<T>> actionRedirectedTo)
58+
{
59+
var methodCall = (MethodCallExpression)actionRedirectedTo.Body;
60+
return ShouldRedirectTo(methodCall.Method);
61+
}
62+
63+
public RouteValueDictionary ShouldRedirectTo(MethodInfo method, RouteValueDictionary expectedValues = null)
64+
{
65+
ValidateActionReturnType<RedirectToRouteResult>();
66+
67+
var controllerName = new Regex(@"Controller$").Replace(typeof(T).Name, "");
68+
var actionName = method.Name;
69+
var redirectResult = (RedirectToRouteResult)_actionResult;
70+
71+
if (redirectResult.RouteValues.ContainsKey("Controller") && redirectResult.RouteValues["Controller"].ToString() != controllerName)
72+
throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"]));
73+
74+
if (!redirectResult.RouteValues.ContainsKey("Action"))
75+
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", actionName));
76+
77+
if (redirectResult.RouteValues["Action"].ToString() != actionName)
78+
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"]));
79+
80+
if (expectedValues == null)
81+
return redirectResult.RouteValues;
82+
83+
foreach (var expectedRouteValue in expectedValues)
84+
{
85+
object actualValue;
86+
if (!redirectResult.RouteValues.TryGetValue(expectedRouteValue.Key, out actualValue))
87+
{
88+
throw new ActionResultAssertionException(string.Format("Expected redirect to have parameter '{0}', but it did not.", expectedRouteValue.Key));
89+
}
90+
if (actualValue.ToString() != expectedRouteValue.Value.ToString())
91+
{
92+
throw new ActionResultAssertionException(
93+
string.Format("Expected parameter '{0}' to have value '{1}', but instead was given value '{2}'."
94+
, expectedRouteValue.Key
95+
, expectedRouteValue.Value
96+
, actualValue
97+
));
98+
}
99+
}
100+
101+
return redirectResult.RouteValues;
102+
}
103+
104+
public RouteValueDictionary ShouldRedirectTo<TController>(Expression<Action<TController>> actionRedirectedTo) where TController : Controller
105+
{
106+
var methodCall = (MethodCallExpression)actionRedirectedTo.Body;
107+
return ShouldRedirectTo<TController>(methodCall.Method);
108+
}
109+
110+
public RouteValueDictionary ShouldRedirectTo<TController>(MethodInfo methodInfo) where TController : Controller
111+
{
112+
ValidateActionReturnType<RedirectToRouteResult>();
113+
114+
var controllerName = new Regex(@"Controller$").Replace(typeof(TController).Name, "");
115+
var actionName = methodInfo.Name;
116+
117+
var redirectResult = (RedirectToRouteResult)_actionResult;
118+
119+
if (redirectResult.RouteValues["Controller"] == null)
120+
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}' in '{1}' controller, but instead was given redirect to action '{2}' within the same controller.", actionName, controllerName, redirectResult.RouteValues["Action"]));
121+
122+
if (redirectResult.RouteValues["Controller"].ToString() != controllerName)
123+
throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"]));
124+
125+
if (redirectResult.RouteValues["Action"].ToString() != actionName)
126+
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"]));
127+
128+
return redirectResult.RouteValues;
129+
}
130+
}
131+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Web.Mvc;
5+
6+
namespace TestStack.FluentMVCTesting
7+
{
8+
public partial class ControllerResultTest<T>
9+
{
10+
private static void EnsureContentTypeIsSame(string actual, string expected)
11+
{
12+
if (expected == null) return;
13+
if (actual != expected)
14+
{
15+
throw new ActionResultAssertionException(string.Format(
16+
"Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual));
17+
}
18+
}
19+
20+
private static byte[] ConvertStreamToArray(Stream stream)
21+
{
22+
using (var memoryStream = new MemoryStream())
23+
{
24+
stream.CopyTo(memoryStream);
25+
stream.Position = 0;
26+
return memoryStream.ToArray();
27+
}
28+
}
29+
30+
public FileResult ShouldRenderAnyFile(string contentType = null)
31+
{
32+
ValidateActionReturnType<FileResult>();
33+
var fileResult = (FileResult)_actionResult;
34+
35+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
36+
37+
return fileResult;
38+
}
39+
40+
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
41+
{
42+
ValidateActionReturnType<FileContentResult>();
43+
var fileResult = (FileContentResult)_actionResult;
44+
45+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
46+
47+
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
48+
{
49+
throw new ActionResultAssertionException(string.Format(
50+
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
51+
string.Join(", ", contents),
52+
string.Join(", ", fileResult.FileContents)));
53+
}
54+
55+
return fileResult;
56+
}
57+
58+
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
59+
{
60+
ValidateActionReturnType<FileContentResult>();
61+
var fileResult = (FileContentResult)_actionResult;
62+
63+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
64+
65+
if (encoding == null)
66+
encoding = Encoding.UTF8;
67+
68+
var reconstitutedText = encoding.GetString(fileResult.FileContents);
69+
if (contents != reconstitutedText)
70+
{
71+
throw new ActionResultAssertionException(string.Format(
72+
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
73+
contents,
74+
reconstitutedText));
75+
}
76+
77+
return fileResult;
78+
}
79+
80+
public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
81+
{
82+
var reconstitutedStream = new MemoryStream(content);
83+
return ShouldRenderFileStream(reconstitutedStream, contentType);
84+
}
85+
86+
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
87+
{
88+
ValidateActionReturnType<FileStreamResult>();
89+
var fileResult = (FileStreamResult)_actionResult;
90+
91+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
92+
93+
if (stream != null)
94+
{
95+
byte[] expected = ConvertStreamToArray(stream);
96+
byte[] actual = ConvertStreamToArray(fileResult.FileStream);
97+
98+
if (!expected.SequenceEqual(actual))
99+
{
100+
throw new ActionResultAssertionException(string.Format(
101+
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
102+
string.Join(", ", expected),
103+
string.Join(", ", actual)));
104+
}
105+
}
106+
107+
return fileResult;
108+
}
109+
110+
public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null)
111+
{
112+
ValidateActionReturnType<FileStreamResult>();
113+
var fileResult = (FileStreamResult)_actionResult;
114+
115+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
116+
117+
if (encoding == null)
118+
encoding = Encoding.UTF8;
119+
120+
var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream));
121+
if (contents != reconstitutedText)
122+
{
123+
throw new ActionResultAssertionException(string.Format(
124+
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
125+
contents,
126+
reconstitutedText));
127+
}
128+
129+
return fileResult;
130+
}
131+
132+
public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null)
133+
{
134+
ValidateActionReturnType<FilePathResult>();
135+
var fileResult = (FilePathResult)_actionResult;
136+
137+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
138+
139+
if (fileName != null && fileName != fileResult.FileName)
140+
{
141+
throw new ActionResultAssertionException(string.Format(
142+
"Expected file name to be '{0}', but instead was given '{1}'.",
143+
fileName,
144+
fileResult.FileName));
145+
}
146+
147+
return fileResult;
148+
}
149+
}
150+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Web.Mvc;
2+
3+
namespace TestStack.FluentMVCTesting
4+
{
5+
public partial class ControllerResultTest<T>
6+
{
7+
private ViewResultTest ShouldRenderViewResult<TViewResult>(string viewName) where TViewResult : ViewResultBase
8+
{
9+
ValidateActionReturnType<TViewResult>();
10+
11+
var viewResult = (TViewResult)_actionResult;
12+
13+
if (viewResult.ViewName != viewName && (viewName != _actionName || viewResult.ViewName != ""))
14+
{
15+
throw new ActionResultAssertionException(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", viewName, viewResult.ViewName == "" ? _actionName : viewResult.ViewName));
16+
}
17+
18+
return new ViewResultTest(viewResult, _controller);
19+
}
20+
21+
public ViewResultTest ShouldRenderView(string viewName)
22+
{
23+
return ShouldRenderViewResult<ViewResult>(viewName);
24+
}
25+
26+
public ViewResultTest ShouldRenderPartialView(string viewName)
27+
{
28+
return ShouldRenderViewResult<PartialViewResult>(viewName);
29+
}
30+
31+
public ViewResultTest ShouldRenderDefaultView()
32+
{
33+
return ShouldRenderView(_actionName);
34+
}
35+
36+
public ViewResultTest ShouldRenderDefaultPartialView()
37+
{
38+
return ShouldRenderPartialView(_actionName);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)