Skip to content

Commit 0292674

Browse files
committed
Added MyController class allowing direct testing of controllers (#133)
1 parent 3a0683e commit 0292674

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{09353A03-2B0C-496B-8EB1-2CB6A22D758B}"
77
EndProject

src/MyTested.AspNetCore.Mvc.Core/Internal/Application/TestApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal static void LoadPlugins()
196196

197197
internal static void TryInitialize()
198198
{
199-
if (TestConfiguration.AutomaticStartup)
199+
if (!initialiazed && TestConfiguration.AutomaticStartup)
200200
{
201201
startupType = TryFindDefaultStartupType();
202202

src/MyTested.AspNetCore.Mvc.Core/Internal/TestLocal.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
using System.Threading;
88
#endif
99

10-
public class TestLocal<T>
11-
where T : class
10+
public class TestLocal<T> where T : class
1211
{
1312
#if NET451
1413
private static readonly string Key = $"__{typeof(T).FullName}_Current__";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System;
4+
using Builders.Contracts.Controllers;
5+
using Builders.Controllers;
6+
using Internal.Application;
7+
using Internal.TestContexts;
8+
9+
public class MyController<TController> : ControllerBuilder<TController>
10+
where TController : class
11+
{
12+
static MyController()
13+
{
14+
TestApplication.TryInitialize();
15+
}
16+
17+
public MyController()
18+
: this((TController)null)
19+
{
20+
}
21+
22+
public MyController(TController controller)
23+
: this(() => controller)
24+
{
25+
}
26+
27+
public MyController(Func<TController> construction)
28+
: base(new ControllerTestContext { ControllerConstruction = construction })
29+
{
30+
}
31+
32+
public static IControllerBuilder<TController> Instance()
33+
{
34+
return Instance((TController)null);
35+
}
36+
37+
public static IControllerBuilder<TController> Instance(TController controller)
38+
{
39+
return Instance(() => controller);
40+
}
41+
42+
public static IControllerBuilder<TController> Instance(Func<TController> construction)
43+
{
44+
return new ControllerBuilder<TController>(new ControllerTestContext { ControllerConstruction = construction });
45+
}
46+
}
47+
}
File renamed without changes.

0 commit comments

Comments
 (0)