Skip to content

Commit aaaf410

Browse files
authored
Move tests into folders and separate out classes (CarterCommunity#175)
1 parent 64fc544 commit aaaf410

39 files changed

+424
-358
lines changed

test/Carter.Tests/Carter.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.2</TargetFramework>
44
</PropertyGroup>
5-
<ItemGroup>
6-
</ItemGroup>
75
<ItemGroup>
86
<ProjectReference Include="..\..\src\Carter.csproj" />
97
</ItemGroup>

test/Carter.Tests/CarterConfiguratorTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Carter.Tests
22
{
3+
using Carter.Tests.ContentNegotiation;
4+
using Carter.Tests.Modelbinding;
5+
using Carter.Tests.StatusCodeHandlers;
36
using Xunit;
47

58
public class CarterConfiguratorTests

test/Carter.Tests/CarterExtensionTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
namespace Carter.Tests
22
{
33
using System.Linq;
4+
using Carter.Tests.ContentNegotiation;
5+
using Carter.Tests.Modelbinding;
6+
using Carter.Tests.StatusCodeHandlers;
47
using FluentValidation;
58
using Microsoft.Extensions.DependencyInjection;
69
using Xunit;

test/Carter.Tests/CarterModuleSetupTests.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/Carter.Tests/NegotiatorModule.cs renamed to test/Carter.Tests/ContentNegotiation/NegotiatorModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Carter.Tests
1+
namespace Carter.Tests.ContentNegotiation
22
{
33
using Carter.Response;
44

test/Carter.Tests/ResponseNegotiatorTests.cs renamed to test/Carter.Tests/ContentNegotiation/ResponseNegotiatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Carter.Tests
1+
namespace Carter.Tests.ContentNegotiation
22
{
33
using System;
44
using System.Net.Http;
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,84 @@
1-
namespace Carter.Tests
2-
{
3-
using System;
4-
using System.Linq;
5-
using System.Net.Http;
6-
using System.Threading.Tasks;
7-
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.TestHost;
10-
using Microsoft.Extensions.DependencyInjection;
11-
using Xunit;
12-
13-
public class ModuleLifetimeTests
14-
{
15-
/*
16-
17-
* Transient objects are always different; a new instance is provided to every controller and every service.
18-
19-
* Scoped objects are the same within a request, but different across different requests.
20-
21-
* Singleton objects are the same for every object and every request.
22-
23-
*/
24-
private TestServer server;
25-
26-
private HttpClient httpClient;
27-
28-
private void ConfigureServer()
29-
{
30-
this.server = new TestServer(
31-
new WebHostBuilder()
32-
.ConfigureServices(x =>
33-
{
34-
x.AddScoped<ScopedRequestDependency>();
35-
x.AddScoped<ScopedServiceDependency>();
36-
x.AddTransient<TransientRequestDependency>();
37-
x.AddTransient<TransientServiceDependency>();
38-
x.AddCarter(configurator: c =>
39-
c.WithModule<ScopedRequestDependencyModule>()
40-
.WithModule<TransientRequestDependencyModule>());
41-
})
42-
.Configure(x => x.UseCarter()));
43-
this.httpClient = this.server.CreateClient();
44-
}
45-
46-
[Fact]
47-
public async Task Should_resolve_new_module_foreach_request()
48-
{
49-
this.ConfigureServer();
50-
var first = await this.httpClient.GetStringAsync("/instanceid");
51-
var second = await this.httpClient.GetStringAsync("/instanceid");
52-
53-
Assert.NotEqual(first, second);
54-
}
55-
56-
[Fact]
57-
public async Task Should_resolve_new_scoped_dependency_per_request()
58-
{
59-
this.ConfigureServer();
60-
61-
//Scoped uses the same instance per request so each value separated by a : should be the same
62-
var first = await this.httpClient.GetStringAsync("/scopedreqdep");
63-
Assert.Single(first.Split(":").Distinct());
64-
65-
var second = await this.httpClient.GetStringAsync("/scopedreqdep");
66-
Assert.Single(second.Split(":").Distinct());
67-
68-
Assert.NotEqual(first, second);
69-
}
70-
71-
[Fact]
72-
public async Task Should_resolve_new_transient_dependency_per_request()
73-
{
74-
this.ConfigureServer();
75-
76-
//Transient uses a different instance in each object per request so each value separated by a : should be different
77-
var first = await this.httpClient.GetStringAsync("/transientreqdep");
78-
Assert.Equal(2, first.Split(":").Distinct().Count());
79-
80-
var second = await this.httpClient.GetStringAsync("/transientreqdep");
81-
Assert.Equal(2, second.Split(":").Distinct().Count());
82-
83-
Assert.NotEqual(first, second);
84-
}
85-
}
86-
87-
public class ScopedRequestDependency
88-
{
89-
private string instanceId;
90-
91-
public ScopedRequestDependency()
92-
{
93-
this.instanceId = Guid.NewGuid().ToString();
94-
}
95-
96-
public string GetGuid()
97-
{
98-
return this.instanceId;
99-
}
100-
}
101-
102-
public class ScopedServiceDependency
103-
{
104-
public ScopedServiceDependency(ScopedRequestDependency scopedRequestDependency)
105-
{
106-
this.Guid = scopedRequestDependency.GetGuid();
107-
}
108-
109-
public string Guid { get; set; }
110-
}
111-
112-
public class TransientRequestDependency
113-
{
114-
private readonly string instanceId;
115-
116-
public TransientRequestDependency()
117-
{
118-
this.instanceId = Guid.NewGuid().ToString();
119-
}
120-
121-
public string GetGuid()
122-
{
123-
return instanceId;
124-
}
125-
}
126-
127-
public class TransientServiceDependency
128-
{
129-
public TransientServiceDependency(TransientRequestDependency transientRequestDependency)
130-
{
131-
this.TheGuid = transientRequestDependency.GetGuid();
132-
}
133-
134-
public string TheGuid { get; }
135-
}
136-
137-
public class ScopedRequestDependencyModule : CarterModule
138-
{
139-
private readonly Guid instanceId;
140-
141-
public ScopedRequestDependencyModule(ScopedRequestDependency scopedRequestDependency,
142-
ScopedServiceDependency scopedServiceDependency)
143-
{
144-
this.instanceId = Guid.NewGuid();
145-
this.Get("/scopedreqdep",
146-
ctx => ctx.Response.WriteAsync(scopedRequestDependency.GetGuid() + ":" + scopedServiceDependency.Guid));
147-
this.Get("/instanceid", ctx =>
148-
ctx.Response.WriteAsync(this.instanceId.ToString()));
149-
}
150-
}
151-
152-
public class TransientRequestDependencyModule : CarterModule
153-
{
154-
public TransientRequestDependencyModule(TransientRequestDependency transientRequestDependency,
155-
TransientServiceDependency transientServiceDependency)
156-
{
157-
this.Get("/transientreqdep",
158-
ctx => ctx.Response.WriteAsync(transientRequestDependency.GetGuid() + ":" +
159-
transientServiceDependency.TheGuid));
160-
}
161-
}
162-
}
1+
namespace Carter.Tests.DependencyInjection
2+
{
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.TestHost;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Xunit;
10+
11+
public class ModuleLifetimeTests
12+
{
13+
/*
14+
15+
* Transient objects are always different; a new instance is provided to every controller and every service.
16+
17+
* Scoped objects are the same within a request, but different across different requests.
18+
19+
* Singleton objects are the same for every object and every request.
20+
21+
*/
22+
private TestServer server;
23+
24+
private HttpClient httpClient;
25+
26+
private void ConfigureServer()
27+
{
28+
this.server = new TestServer(
29+
new WebHostBuilder()
30+
.ConfigureServices(x =>
31+
{
32+
x.AddScoped<ScopedRequestDependency>();
33+
x.AddScoped<ScopedServiceDependency>();
34+
x.AddTransient<TransientRequestDependency>();
35+
x.AddTransient<TransientServiceDependency>();
36+
x.AddCarter(configurator: c =>
37+
c.WithModule<ScopedRequestDependencyModule>()
38+
.WithModule<TransientRequestDependencyModule>());
39+
})
40+
.Configure(x => x.UseCarter()));
41+
this.httpClient = this.server.CreateClient();
42+
}
43+
44+
[Fact]
45+
public async Task Should_resolve_new_module_foreach_request()
46+
{
47+
this.ConfigureServer();
48+
var first = await this.httpClient.GetStringAsync("/instanceid");
49+
var second = await this.httpClient.GetStringAsync("/instanceid");
50+
51+
Assert.NotEqual(first, second);
52+
}
53+
54+
[Fact]
55+
public async Task Should_resolve_new_scoped_dependency_per_request()
56+
{
57+
this.ConfigureServer();
58+
59+
//Scoped uses the same instance per request so each value separated by a : should be the same
60+
var first = await this.httpClient.GetStringAsync("/scopedreqdep");
61+
Assert.Single(first.Split(":").Distinct());
62+
63+
var second = await this.httpClient.GetStringAsync("/scopedreqdep");
64+
Assert.Single(second.Split(":").Distinct());
65+
66+
Assert.NotEqual(first, second);
67+
}
68+
69+
[Fact]
70+
public async Task Should_resolve_new_transient_dependency_per_request()
71+
{
72+
this.ConfigureServer();
73+
74+
//Transient uses a different instance in each object per request so each value separated by a : should be different
75+
var first = await this.httpClient.GetStringAsync("/transientreqdep");
76+
Assert.Equal(2, first.Split(":").Distinct().Count());
77+
78+
var second = await this.httpClient.GetStringAsync("/transientreqdep");
79+
Assert.Equal(2, second.Split(":").Distinct().Count());
80+
81+
Assert.NotEqual(first, second);
82+
}
83+
}
84+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Carter.Tests.DependencyInjection
2+
{
3+
using System;
4+
5+
public class ScopedRequestDependency
6+
{
7+
private string instanceId;
8+
9+
public ScopedRequestDependency()
10+
{
11+
this.instanceId = Guid.NewGuid().ToString();
12+
}
13+
14+
public string GetGuid()
15+
{
16+
return this.instanceId;
17+
}
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Carter.Tests.DependencyInjection
2+
{
3+
using System;
4+
using Microsoft.AspNetCore.Http;
5+
6+
public class ScopedRequestDependencyModule : CarterModule
7+
{
8+
private readonly Guid instanceId;
9+
10+
public ScopedRequestDependencyModule(ScopedRequestDependency scopedRequestDependency,
11+
ScopedServiceDependency scopedServiceDependency)
12+
{
13+
this.instanceId = Guid.NewGuid();
14+
this.Get("/scopedreqdep",
15+
ctx => ctx.Response.WriteAsync(scopedRequestDependency.GetGuid() + ":" + scopedServiceDependency.Guid));
16+
this.Get("/instanceid", ctx =>
17+
ctx.Response.WriteAsync(this.instanceId.ToString()));
18+
}
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Carter.Tests.DependencyInjection
2+
{
3+
public class ScopedServiceDependency
4+
{
5+
public ScopedServiceDependency(ScopedRequestDependency scopedRequestDependency)
6+
{
7+
this.Guid = scopedRequestDependency.GetGuid();
8+
}
9+
10+
public string Guid { get; set; }
11+
}
12+
}

0 commit comments

Comments
 (0)