-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathControllerExtensions.cs
More file actions
122 lines (95 loc) · 4.96 KB
/
ControllerExtensions.cs
File metadata and controls
122 lines (95 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace TestStack.FluentMVCTesting
{
public static class ControllerExtensions
{
public static T WithModelErrors<T>(this T controller) where T : Controller
{
controller.ModelState.AddModelError("Key", "Value");
return controller;
}
public static ControllerResultTest<T> WithCallTo<T, TAction>(this T controller, Expression<Func<T, TAction>> actionCall)
where T : Controller
where TAction : ActionResult
{
var actionName = ((MethodCallExpression)actionCall.Body).Method.Name;
var actionResult = actionCall.Compile().Invoke(controller);
return new ControllerResultTest<T>(controller, actionName, actionResult);
}
public static ControllerResultTest<T> WithCallTo<T, TAction>(this T controller, Expression<Func<T, Task<TAction>>> actionCall)
where T : Controller
where TAction : ActionResult
{
var actionName = ((MethodCallExpression)actionCall.Body).Method.Name;
var actionResult = actionCall.Compile().Invoke(controller).Result;
return new ControllerResultTest<T>(controller, actionName, actionResult);
}
public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T controller, Expression<Func<T, TAction>> actionCall)
where T : Controller
where TAction : ActionResult
{
var action = ((MethodCallExpression)actionCall.Body).Method;
if (!action.IsDefined(typeof(ChildActionOnlyAttribute), false))
throw new InvalidControllerActionException(string.Format("Expected action {0} of controller {1} to be a child action, but it didn't have the ChildActionOnly attribute.", action.Name, controller.GetType().Name));
return controller.WithCallTo(actionCall);
}
public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T controller, Expression<Func<T, Task<TAction>>> actionCall)
where T : Controller
where TAction : ActionResult
{
var action = ((MethodCallExpression)actionCall.Body).Method;
if (!action.IsDefined(typeof(ChildActionOnlyAttribute), false))
throw new InvalidControllerActionException(string.Format("Expected action {0} of controller {1} to be a child action, but it didn't have the ChildActionOnly attribute.", action.Name, controller.GetType().Name));
return controller.WithCallTo(actionCall);
}
public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null)
{
var actual = controller.TempData[key];
if (actual == null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
}
if (value != null && actual.GetType() != value.GetType())
{
throw new TempDataAssertionException(string.Format(
"Expected value to be of type {0}, but instead was {1}.",
value.GetType().FullName,
actual.GetType().FullName));
}
if (value != null && !value.Equals(actual))
{
throw new TempDataAssertionException(string.Format(
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
}
return new TempDataResultTest(controller);
}
public static TempDataResultTest ShouldHaveTempDataProperty<TValue>(this ControllerBase controller, string key, Func<TValue, bool> predicate)
{
var actual = controller.TempData[key];
if (actual == null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
}
if (!predicate((TValue)actual))
{
throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed.");
}
return new TempDataResultTest(controller);
}
public static TempDataResultTest ShouldNotHaveTempDataProperty(this Controller controller, string key)
{
var actual = controller.TempData[key];
if (actual != null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have no value with key \"{0}\", but found one.", key));
}
return new TempDataResultTest(controller);
}
}
}