Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit a3bc8d2

Browse files
pietersapMassimoC
authored andcommitted
#112 Use XUnit fixture for integrationtest (#114)
* create test server fixture class * XUnit fixture in integration test * empty spaces
1 parent 031446c commit a3bc8d2

File tree

5 files changed

+85
-65
lines changed

5 files changed

+85
-65
lines changed

maturity-level-two/tests/Codit.IntegrationTest/CarTest.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Xunit;
2-
using Microsoft.AspNetCore.TestHost;
3-
using Microsoft.AspNetCore.Hosting;
2+
//using Microsoft.AspNetCore.TestHost;
3+
//using Microsoft.AspNetCore.Hosting;
44
using System.Net.Http;
55
using System.Net;
66
using System.Threading.Tasks;
@@ -10,17 +10,14 @@
1010

1111
namespace Codit.IntegrationTest
1212
{
13+
[Collection("TestServer")]
1314
public class CarTest
1415
{
15-
private readonly HttpClient _httpClient;
16-
public CarTest()
17-
{
18-
if (_httpClient != null) return;
19-
var srv = new TestServer(new WebHostBuilder()
20-
.UseEnvironment("Development")
21-
.UseStartup<Startup>());
16+
TestServerFixture fixture;
2217

23-
_httpClient = srv.CreateClient();
18+
public CarTest(TestServerFixture fixture)
19+
{
20+
this.fixture = fixture;
2421
}
2522

2623
[Fact]
@@ -29,7 +26,7 @@ public async Task GetCars_Ok_TestAsync()
2926
//Arrange
3027
var request = new HttpRequestMessage(HttpMethod.Get, "/codito/v1/car");
3128
//Act
32-
var response = await _httpClient.SendAsync(request);
29+
var response = await fixture._httpClient.SendAsync(request);
3330
//Assert
3431
response.StatusCode.Should().Be(HttpStatusCode.OK);
3532
}
@@ -41,7 +38,7 @@ public async Task GetSingleTeam_Ok_TestAsync()
4138
int id = 1;
4239
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/car/{id}");
4340
//Act
44-
var response = await _httpClient.SendAsync(request);
41+
var response = await fixture._httpClient.SendAsync(request);
4542
//Arrange
4643
response.StatusCode.Should().Be(HttpStatusCode.OK);
4744
}
@@ -53,7 +50,7 @@ public async Task GetSingleTeam_NotFound_TestAsync()
5350
int id = -1;
5451
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/car/{id}");
5552
//Act
56-
var response = await _httpClient.SendAsync(request);
53+
var response = await fixture._httpClient.SendAsync(request);
5754
//Assert
5855
response.Content.Headers.Should().NotBeNullOrEmpty();
5956
response.StatusCode.Should().Be(HttpStatusCode.NotFound);

maturity-level-two/tests/Codit.IntegrationTest/CustomizationTest.cs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212

1313
namespace Codit.IntegrationTest
1414
{
15+
[Collection("TestServer")]
1516
public class CustomizationTest
1617
{
17-
private readonly HttpClient _httpClient;
18-
public CustomizationTest()
19-
{
20-
if (_httpClient != null) return;
21-
var srv = new TestServer(new WebHostBuilder()
22-
.UseEnvironment("Development")
23-
.UseStartup<Startup>());
18+
TestServerFixture fixture;
2419

25-
_httpClient = srv.CreateClient();
20+
public CustomizationTest(TestServerFixture fixture)
21+
{
22+
this.fixture = fixture;
2623
}
2724

2825
[Fact]
@@ -31,7 +28,7 @@ public async Task GetCustomizations_Ok_TestAsync()
3128
//Arrange
3229
var request = new HttpRequestMessage(HttpMethod.Get, "/codito/v1/customization");
3330
//Act
34-
var response = await _httpClient.SendAsync(request);
31+
var response = await fixture._httpClient.SendAsync(request);
3532
//Assert
3633
response.StatusCode.Should().Be(HttpStatusCode.OK);
3734
}
@@ -43,7 +40,7 @@ public async Task GetSingleCustomization_Ok_TestAsync()
4340
int id = 1;
4441
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
4542
//Act
46-
var response = await _httpClient.SendAsync(request);
43+
var response = await fixture._httpClient.SendAsync(request);
4744
//Assert
4845
response.StatusCode.Should().Be(HttpStatusCode.OK);
4946
}
@@ -56,7 +53,7 @@ public async Task GetSingleCustomiation_NotFound_TestAsync()
5653
int id = -1;
5754
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
5855
//Act
59-
var response = await _httpClient.SendAsync(request);
56+
var response = await fixture._httpClient.SendAsync(request);
6057
//Assert
6158
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
6259
}
@@ -74,7 +71,7 @@ public async Task CreateNewCustomization_Created_TestAsync()
7471
};
7572
var request = TestExtensions.GetJsonRequest(customization, "POST", $"/codito/v1/customization/");
7673
//Act
77-
var response = await _httpClient.SendAsync(request);
74+
var response = await fixture._httpClient.SendAsync(request);
7875
//Assert
7976
response.StatusCode.Should().Be(HttpStatusCode.Created);
8077
}
@@ -91,39 +88,39 @@ public async Task CreateNewCustomization_BadRequest_TestAsync()
9188
};
9289
var request = TestExtensions.GetJsonRequest(newCustomization, "POST", "/codito/v1/customization/");
9390
//Act
94-
var response = await _httpClient.SendAsync(request);
91+
var response = await fixture._httpClient.SendAsync(request);
9592
//Assert
9693
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
9794

9895
}
9996

10097
[Fact]
101-
public async Task DeleteCustomization_NoContent_TestAsync()
98+
public async Task DeleteCustomization_NotFound_TestAsync()
10299
{
103100
//Arrange
104101
int id = -1;
105102
var request = new HttpRequestMessage(HttpMethod.Delete, $"/codito/v1/customization/{id}");
106103
//Act
107-
var response = await _httpClient.SendAsync(request);
104+
var response = await fixture._httpClient.SendAsync(request);
108105
//Assert
109106
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
110107

111108
}
112109

113110
[Fact]
114-
public async Task DeleteCustomization_NotFound_TestAsync()
111+
public async Task DeleteCustomization_NoContent_TestAsync()
115112
{
116113
//Arrange
117-
int id = 1;
114+
int id = 3;
118115
var request = new HttpRequestMessage(HttpMethod.Delete, $"/codito/v1/customization/{id}");
119116
//Act
120-
var response = await _httpClient.SendAsync(request);
117+
var response = await fixture._httpClient.SendAsync(request);
121118

122119
//Assert
123120
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
124121

125122
request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
126-
response = await _httpClient.SendAsync(request);
123+
response = await fixture._httpClient.SendAsync(request);
127124
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
128125
}
129126

@@ -134,17 +131,17 @@ public async Task SellCustomization_Accepted_TestAsync()
134131
int id = 1;
135132

136133
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
137-
var response = await _httpClient.SendAsync(request);
134+
var response = await fixture._httpClient.SendAsync(request);
138135
var actualDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
139136

140137
request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");
141138
//Act
142-
response = await _httpClient.SendAsync(request);
139+
response = await fixture._httpClient.SendAsync(request);
143140
//Assert
144141
response.StatusCode.Should().Be(HttpStatusCode.Accepted);
145142
// (Inventory must be decremented, number of sales incremented.
146143
request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
147-
response = await _httpClient.SendAsync(request);
144+
response = await fixture._httpClient.SendAsync(request);
148145
var updatedDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
149146

150147
updatedDto.InventoryLevel.Should().Be(actualDto.InventoryLevel - 1);
@@ -159,7 +156,7 @@ public async Task SellCustomization_NotFound_TestAsync()
159156
int id = -1;
160157
var request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");
161158
//Act
162-
var response = await _httpClient.SendAsync(request);
159+
var response = await fixture._httpClient.SendAsync(request);
163160
//Assert
164161
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
165162
}
@@ -176,7 +173,7 @@ public async Task SellCustomization_SoldOutBadRequest_TestAsync()
176173
};
177174

178175
var request = TestExtensions.GetJsonRequest(newCustomization, "POST", "/codito/v1/customization/");
179-
var response = await _httpClient.SendAsync(request);
176+
var response = await fixture._httpClient.SendAsync(request);
180177
response.StatusCode.Should().Be(HttpStatusCode.Created);
181178
//(Get Id of this new customization)
182179
var newDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
@@ -185,7 +182,7 @@ public async Task SellCustomization_SoldOutBadRequest_TestAsync()
185182
request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");
186183

187184
//Act
188-
response = await _httpClient.SendAsync(request);
185+
response = await fixture._httpClient.SendAsync(request);
189186

190187
//Assert
191188
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
@@ -204,7 +201,7 @@ public async Task UpdateCustomizationIncremental_NotFound_TestAsync()
204201
var request = TestExtensions.GetJsonRequest(customization, "PATCH", $"/codito/v1/customization/{id}");
205202

206203
// Act
207-
var response = await _httpClient.SendAsync(request);
204+
var response = await fixture._httpClient.SendAsync(request);
208205

209206
//Assert
210207
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
@@ -221,19 +218,19 @@ public async Task UpdateCustomizationIncremental_NoContent_TestAsync()
221218
};
222219

223220
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
224-
var response = await _httpClient.SendAsync(request);
221+
var response = await fixture._httpClient.SendAsync(request);
225222
var actualDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
226223
request = TestExtensions.GetJsonRequest(customization, "PATCH", $"/codito/v1/customization/{id}");
227224

228225
//actualDto.Should().BeNull();
229226

230227
// Act
231-
response = await _httpClient.SendAsync(request);
228+
response = await fixture._httpClient.SendAsync(request);
232229

233230
//Assert
234231
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
235232
request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
236-
response = await _httpClient.SendAsync(request);
233+
response = await fixture._httpClient.SendAsync(request);
237234
var updatedDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
238235
updatedDto.Id.Should().Be(actualDto.Id);
239236

maturity-level-two/tests/Codit.IntegrationTest/OpenApiTest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111

1212
namespace Codit.IntegrationTest
1313
{
14+
[Collection("TestServer")]
1415
public class OpenApiTest
1516
{
16-
private readonly HttpClient _httpClient;
17-
public OpenApiTest()
18-
{
19-
if (_httpClient != null) return;
20-
var srv = new TestServer(new WebHostBuilder()
21-
.UseEnvironment("Development")
22-
.UseStartup<Startup>());
17+
TestServerFixture fixture;
2318

24-
_httpClient = srv.CreateClient();
19+
public OpenApiTest(TestServerFixture fixture)
20+
{
21+
this.fixture = fixture;
2522
}
2623

2724
[Fact]
@@ -30,7 +27,7 @@ public async Task OpenApi_OperationId_Test()
3027
//Arrange
3128
var request = new HttpRequestMessage(new HttpMethod("GET"), "/swagger/v1/swagger.json");
3229
//Act
33-
var response = await _httpClient.SendAsync(request);
30+
var response = await fixture._httpClient.SendAsync(request);
3431
//Assert
3532
response.StatusCode.Should().Be(HttpStatusCode.OK);
3633

maturity-level-two/tests/Codit.IntegrationTest/ProblemJsonTest.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

1515
namespace Codit.IntegrationTest
1616
{
17+
[Collection("TestServer")]
1718
public class ProblemJsonTest
1819
{
19-
private readonly HttpClient _httpClient;
20-
public ProblemJsonTest()
21-
{
22-
if (_httpClient != null) { return; }
23-
var srv = new TestServer(new WebHostBuilder()
24-
.UseEnvironment("Development")
25-
.UseStartup<Startup>());
20+
TestServerFixture fixture;
2621

27-
_httpClient = srv.CreateClient();
22+
public ProblemJsonTest(TestServerFixture fixture)
23+
{
24+
this.fixture = fixture;
2825
}
2926

3027
[Fact]
@@ -33,7 +30,7 @@ public async Task ProblemJson_RouteNotExists_404_Test()
3330
//Arrange
3431
var request = new HttpRequestMessage(new HttpMethod("GET"), "/codito/api/v1/car");
3532
//Act
36-
var response = await _httpClient.SendAsync(request);
33+
var response = await fixture._httpClient.SendAsync(request);
3734
//Assert
3835
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
3936
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
@@ -50,7 +47,7 @@ public async Task ProblemJson_Validation_400_Test()
5047
};
5148
var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization");
5249
//Act
53-
var response = await _httpClient.SendAsync(request);
50+
var response = await fixture._httpClient.SendAsync(request);
5451
//Assert
5552
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
5653
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
@@ -62,9 +59,9 @@ public async Task ProblemJson_ContentNegotiationNotOk_406_Test()
6259
{
6360
//Arrange
6461
var request = new HttpRequestMessage(new HttpMethod("GET"), "/codito/codito/car");
65-
_httpClient.DefaultRequestHeaders.Add("Accept", "custom/content+type");
62+
request.Headers.Add("Accept", "custom/content+type");
6663
//Act
67-
var response = await _httpClient.SendAsync(request);
64+
var response = await fixture._httpClient.SendAsync(request);
6865
//Assert
6966
response.StatusCode.Should().Be(HttpStatusCode.NotAcceptable);
7067
response.Content.Headers.Should().BeNullOrEmpty(); // With 406 the body is suppressed
@@ -81,7 +78,7 @@ public async Task ProblemJson_UnsupportedContentType_415_Test()
8178
};
8279
var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization/", "application/pdf");
8380
//Act
84-
var response = await _httpClient.SendAsync(request);
81+
var response = await fixture._httpClient.SendAsync(request);
8582
//Assert
8683
response.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
8784
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
@@ -100,7 +97,7 @@ public async Task ProblemJson_InternalServerError_500_Test()
10097

10198
var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization");
10299
//Act
103-
var response = await _httpClient.SendAsync(request);
100+
var response = await fixture._httpClient.SendAsync(request);
104101
//Assert
105102
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
106103
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Codit.LevelTwo;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.TestHost;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.Http;
7+
using System.Text;
8+
using Xunit;
9+
10+
namespace Codit.IntegrationTest
11+
{
12+
public class TestServerFixture
13+
{
14+
internal readonly HttpClient _httpClient;
15+
16+
public TestServerFixture()
17+
{
18+
if (_httpClient != null) return;
19+
var srv = new TestServer(new WebHostBuilder()
20+
.UseEnvironment("Development")
21+
.UseStartup<Startup>());
22+
23+
_httpClient = srv.CreateClient();
24+
25+
}
26+
}
27+
28+
[CollectionDefinition("TestServer")]
29+
public class TestServerCollection : ICollectionFixture<TestServerFixture>
30+
{
31+
}
32+
}

0 commit comments

Comments
 (0)