-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathControllerTester.cs
118 lines (103 loc) · 5.31 KB
/
ControllerTester.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
115
116
117
118
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace SparkyTestHelpers.AspNetMvc
{
/// <summary>
/// ASP.NET Core MVC Controller tester.
/// </summary>
/// <typeparam name="TController">The controller type.</typeparam>
public class ControllerTester<TController> where TController : Controller
{
/// <summary>
/// The <typeparamref name="TController"/> instance being tested.
/// </summary>
public TController Controller { get; private set; }
/// <summary>
/// Creates a new <see cref="ControllerTester{T}" /> instance.
/// </summary>
/// <typeparam name="T">The <see cref="System.Web.Mvc.Controller" /> type.</typeparam>
/// <param name="controller">THe <see cref="System.Web.Mvc.Controller"/> to be tested.</param>
/// <returns>New <see cref="Controller{T}" /> instance.</returns>
public static ControllerTester<T> CreateTester<T>(T controller) where T : Controller
{
return new ControllerTester<T>(controller);
}
/// <summary>
/// Creates new <see cref="ControllerTester{TController}"/> instance.
/// </summary>
/// <param name="controller">The controller.</param>
public ControllerTester(TController controller)
{
Controller = controller;
}
/// <summary>
/// Creates a new <see cref="ControllerActionTester"/> instance.
/// </summary>
/// <param name="actionResultProvider">Function that returns an <see cref="ActionResult"/>.</param>
/// <returns>New <see cref="ControllerActionTester"/> instance.</returns>
public ControllerActionTester Action(Func<ActionResult> actionResultProvider) => Action(_ => actionResultProvider);
/// <summary>
/// Creates a new <see cref="ControllerActionTester"/> instance.
/// </summary>
/// <param name="expression">Expression to specify the controller action to be tested.</param>
/// <returns>New <see cref="ControllerActionTester"/> instance.</returns>
public ControllerActionTester Action(Expression<Func<TController, Func<ActionResult>>> expression)
{
var func = expression.Compile();
return new ControllerActionTester(Controller, func(Controller));
}
/// <summary>
/// Creates a new <see cref="ControllerActionTester"/> instance for an async action.
/// </summary>
/// <param name="expression">Expression to specify the async controller action to be tested.</param>
/// <returns>New <see cref="ControllerActionTester"/> instance.</returns>
public ControllerActionTester Action(Expression<Func<TController, Func<Task<ActionResult>>>> expression)
{
Func<TController, Func<Task<ActionResult>>> funcWithTask = expression.Compile();
Func<ActionResult> func = () =>
{
Task<ActionResult> task = funcWithTask(Controller)();
task.Wait();
return task.Result;
};
return new ControllerActionTester(Controller, func);
}
/// <summary>
/// Creates a new <see cref="ControllerActionTester{TResponse}"/> instance.
/// </summary>
/// <typeparam name="TResponse">The action response type.</typeparam>
/// <param name="actionResultProvider">Function that returns an istance of type <see cref="TResponse"/>.</param>
/// <returns>New <see cref="ControllerActionTester{TController}"/> instance.</returns>
public ControllerActionTester<TResponse> Action<TResponse>(Func<TResponse> actionResultProvider) => Action(_ => actionResultProvider);
/// <summary>
/// Creates a new <see cref="ControllerActionTester{TResponse}"/> instance.
/// </summary>
/// <typeparam name="TResponse">The action response type.</typeparam>
/// <param name="expression">Expression to specify the controller action to be tested.</param>
/// <returns>New <see cref="ControllerActionTester{TResponse}"/> instance.</returns>
public ControllerActionTester<TResponse> Action<TResponse>(Expression<Func<TController, Func<TResponse>>> expression)
{
var func = expression.Compile();
return new ControllerActionTester<TResponse>(Controller, func(Controller));
}
/// <summary>
/// Creates new <see cref="ControllerActionTester{TResponse}"/> instance for an async action.
/// </summary>
/// <typeparam name="TResponse">The action response type.</typeparam>
/// <param name="expression">Expression to specify the async controller action to be tested.</param>
/// <returns>New <see cref="ControllerActionTester{TResponse}"/> instance.</returns>
public ControllerActionTester<TResponse> Action<TResponse>(Expression<Func<TController, Func<Task<TResponse>>>> expression)
{
Func<TController, Func<Task<TResponse>>> funcWithTask = expression.Compile();
Func<TResponse> func = () =>
{
Task<TResponse> task = funcWithTask(Controller)();
task.Wait();
return task.Result;
};
return new ControllerActionTester<TResponse>(Controller, func);
}
}
}