Skip to content

Commit 0152b5c

Browse files
committed
Added support for async controller actions
1 parent d294334 commit 0152b5c

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using TestStack.FluentMVCTesting.Tests.TestControllers;
3+
4+
namespace TestStack.FluentMVCTesting.Tests
5+
{
6+
class AsyncControllersShould
7+
{
8+
private AsyncController _controller;
9+
10+
[SetUp]
11+
public void Setup()
12+
{
13+
_controller = new AsyncController();
14+
}
15+
16+
[Test]
17+
public void Work_correctly_for_valid_check()
18+
{
19+
_controller.WithCallTo(c => c.AsyncViewAction())
20+
.ShouldRenderDefaultView();
21+
}
22+
23+
[Test]
24+
public void Work_correctly_for_invalid_check()
25+
{
26+
Assert.Throws<ActionResultAssertionException>(
27+
() => _controller.WithCallTo(c => c.AsyncViewAction()).ShouldGiveHttpStatus()
28+
);
29+
}
30+
[Test]
31+
public void Work_correctly_for_valid_child_action_check()
32+
{
33+
_controller.WithCallToChild(c => c.AsyncChildViewAction())
34+
.ShouldRenderDefaultView();
35+
}
36+
37+
[Test]
38+
public void Work_correctly_for_invalid_child_action_check()
39+
{
40+
Assert.Throws<InvalidControllerActionException>(
41+
() => _controller.WithCallToChild(c => c.AsyncViewAction())
42+
);
43+
}
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Threading.Tasks;
2+
using System.Web.Mvc;
3+
4+
namespace TestStack.FluentMVCTesting.Tests.TestControllers
5+
{
6+
class AsyncController : Controller
7+
{
8+
public Task<ActionResult> AsyncViewAction()
9+
{
10+
// Task.FromResult would be better, but I want to support .NET 4
11+
return Task.Factory.StartNew<ActionResult>(() => View());
12+
}
13+
14+
[ChildActionOnly]
15+
public Task<ActionResult> AsyncChildViewAction()
16+
{
17+
return Task.Factory.StartNew<ActionResult>(() => View());
18+
}
19+
}
20+
}

TestStack.FluentMVCTesting.Tests/TestStack.FluentMVCTesting.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
<Reference Include="System.Xml" />
7878
</ItemGroup>
7979
<ItemGroup>
80+
<Compile Include="AsyncControllerTests.cs" />
81+
<Compile Include="TestControllers\AsyncController.cs" />
8082
<Compile Include="ControllerExtensionsTests.cs" />
8183
<Compile Include="ControllerResultTestTests.cs" />
8284
<Compile Include="ModelTestTests.cs" />

TestStack.FluentMvcTesting/ControllerExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq.Expressions;
3+
using System.Threading.Tasks;
34
using System.Web.Mvc;
45

56
namespace TestStack.FluentMVCTesting
@@ -24,6 +25,17 @@ public static ControllerResultTest<T> WithCallTo<T, TAction>(this T controller,
2425
return new ControllerResultTest<T>(controller, actionName, actionResult);
2526
}
2627

28+
public static ControllerResultTest<T> WithCallTo<T, TAction>(this T controller, Expression<Func<T, Task<TAction>>> actionCall)
29+
where T : Controller
30+
where TAction : ActionResult
31+
{
32+
var actionName = ((MethodCallExpression)actionCall.Body).Method.Name;
33+
34+
var actionResult = actionCall.Compile().Invoke(controller).Result;
35+
36+
return new ControllerResultTest<T>(controller, actionName, actionResult);
37+
}
38+
2739
public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T controller, Expression<Func<T, TAction>> actionCall)
2840
where T : Controller
2941
where TAction : ActionResult
@@ -35,5 +47,17 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
3547

3648
return controller.WithCallTo(actionCall);
3749
}
50+
51+
public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T controller, Expression<Func<T, Task<TAction>>> actionCall)
52+
where T : Controller
53+
where TAction : ActionResult
54+
{
55+
var action = ((MethodCallExpression)actionCall.Body).Method;
56+
57+
if (!action.IsDefined(typeof(ChildActionOnlyAttribute), false))
58+
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));
59+
60+
return controller.WithCallTo(actionCall);
61+
}
3862
}
3963
}

0 commit comments

Comments
 (0)