-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathControllerExtensions.cs
114 lines (88 loc) · 4.68 KB
/
ControllerExtensions.cs
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
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("Expected action \{action.Name} of controller \{controller.GetType().Name} to be a child action, but it didn't have the ChildActionOnly attribute.");
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("Expected action \{action.Name} of controller \{controller.GetType().Name} to be a child action, but it didn't have the ChildActionOnly attribute.");
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("Expected TempData to have a non-null value with key \"\{key}\", but none found.");
}
if (value != null && actual.GetType() != value.GetType())
{
throw new TempDataAssertionException("Expected value to be of type \{value.GetType().FullName}, but instead was \{actual.GetType().FullName}.");
}
if (value != null && !value.Equals(actual))
{
throw new TempDataAssertionException("Expected value for key \"\{key}\" to be \"\{value}\", but instead found \"\{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("Expected TempData to have a non-null value with key \"\{key}\", but none found.");
}
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("Expected TempData to have no value with key \"\{key}\", but found one.");
}
return new TempDataResultTest(controller);
}
}
}