-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathControllerExtensions.cs
More file actions
40 lines (32 loc) · 1.5 KB
/
ControllerExtensions.cs
File metadata and controls
40 lines (32 loc) · 1.5 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
//
using System;
using System.Linq.Expressions;
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> 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);
}
}
}