Skip to content

Commit e915ced

Browse files
committed
Multiple startups now have to be turned on in the testconfig.json file (#135 #168)
1 parent 1788de7 commit e915ced

File tree

27 files changed

+116
-11
lines changed

27 files changed

+116
-11
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ internal static Type StartupType
115115

116116
set
117117
{
118+
if (startupType != null && TestConfiguration.General.AsynchronousTests)
119+
{
120+
throw new InvalidOperationException("Multiple Startup types per test project while running asynchronous tests is not supported. Either set 'General.AsynchronousTests' in the 'testconfig.json' file to 'false' or separate your tests into different test projects. The latter is recommended. If you choose the first option, you may need to disable asynchronous testing in your preferred test runner too.");
121+
}
122+
118123
if (initialiazed)
119124
{
120125
Reset();

src/MyTested.AspNetCore.Mvc.Configuration/Internal/Configuration/GeneralTestConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class GeneralTestConfiguration : BaseConfiguration
66
{
7+
private const string AsynchronousTestsConfigKey = "AsynchronousTests";
78
private const string WebAssemblyNameConfigKey = "WebAssemblyName";
89
private const string TestAssemblyNameConfigKey = "TestAssemblyName";
910
private const string AutomaticStartupConfigKey = "AutomaticStartup";
@@ -17,6 +18,8 @@ public GeneralTestConfiguration(IConfiguration configuration)
1718
this.Prefix = "General";
1819
}
1920

21+
public bool AsynchronousTests => this.GetValue(AsynchronousTestsConfigKey, true);
22+
2023
public string WebAssemblyName => this.GetValue(WebAssemblyNameConfigKey);
2124

2225
public string TestAssemblyName => this.GetValue(TestAssemblyNameConfigKey);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MyTested.AspNetCore.Mvc.Plugins
2+
{
3+
using System;
4+
using Internal.TestContexts;
5+
using Utilities;
6+
7+
public class DependencyInjectionTestPlugin : IShouldPassForPlugin
8+
{
9+
private readonly Type serviceProviderType = typeof(IServiceProvider);
10+
11+
public object TryGetValue(Type type, ComponentTestContext testContext)
12+
=> Reflection.AreAssignable(serviceProviderType, type) // ServiceProvider
13+
? testContext.HttpContext.RequestServices
14+
: null;
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"General": {
3+
"AsynchronousTests": false,
34
"AutomaticStartup": false
45
}
56
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"General": {
3+
"AsynchronousTests": false,
34
"AutomaticStartup": false
45
}
56
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2+
"General": {
3+
"AsynchronousTests": false
4+
},
25
"License": "1-3i7E5P3qX5IUWHIAfcXG6DSbOwUBidygp8bnYY/2Rd9zA15SwRWP6QDDp+m/dDTZNBFX2eIHcU/gdcdm83SL695kf3VyvMPw+iyPN6QBh/WnfQwGLqBecrQw+WNPJMz6UgXi2q4e4s/D8/iSjMlwCnzJvC2Yv3zSuADdWObQsygxOjk5OTktMTItMzE6YWRtaW5AbXl0ZXN0ZWRhc3AubmV0Ok15VGVzdGVkLk12YyBUZXN0czpGdWxsOk15VGVzdGVkLkFzcE5ldENvcmUuTXZjLg=="
36
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2+
"General": {
3+
"AsynchronousTests": false
4+
},
25
"License": "1-3i7E5P3qX5IUWHIAfcXG6DSbOwUBidygp8bnYY/2Rd9zA15SwRWP6QDDp+m/dDTZNBFX2eIHcU/gdcdm83SL695kf3VyvMPw+iyPN6QBh/WnfQwGLqBecrQw+WNPJMz6UgXi2q4e4s/D8/iSjMlwCnzJvC2Yv3zSuADdWObQsygxOjk5OTktMTItMzE6YWRtaW5AbXl0ZXN0ZWRhc3AubmV0Ok15VGVzdGVkLk12YyBUZXN0czpGdWxsOk15VGVzdGVkLkFzcE5ldENvcmUuTXZjLg=="
36
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"General": {
3+
"AsynchronousTests": false,
34
"AutomaticStartup": false
45
}
56
}

test/MyTested.AspNetCore.Mvc.DependencyInjection.Test/ServicesTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace MyTested.AspNetCore.Mvc.Test
22
{
3+
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.DependencyInjection.Extensions;
45
using Setups;
56
using Setups.Controllers;
67
using Setups.Services;
8+
using System;
79
using Xunit;
810

911
public class ServicesTests
@@ -50,5 +52,27 @@ public void ScopedServicesShouldRemainThroughTheTestCase()
5052

5153
MyApplication.StartsFrom<DefaultStartup>();
5254
}
55+
56+
[Fact]
57+
public void ShouldPassForShouldWorkCorrectly()
58+
{
59+
MyApplication
60+
.StartsFrom<DefaultStartup>()
61+
.WithServices(services =>
62+
{
63+
services.TryAddScoped<IScopedService, ScopedService>();
64+
});
65+
66+
MyController<ServicesController>
67+
.Instance()
68+
.Calling(c => c.SetValue())
69+
.ShouldPassForThe<IServiceProvider>(services =>
70+
{
71+
var scopedService = services.GetRequiredService<IScopedService>();
72+
Assert.True(scopedService.Value == "Scoped");
73+
});
74+
75+
MyApplication.StartsFrom<DefaultStartup>();
76+
}
5377
}
5478
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"General": {
3+
"AsynchronousTests": false,
34
"AutomaticStartup": false
45
}
56
}

0 commit comments

Comments
 (0)