Skip to content

Commit 4245560

Browse files
authored
Merge pull request #221 from ChrisDoernen/integration-test-example
Add remote calculator project and according tests
2 parents 12d6594 + 559dc46 commit 4245560

17 files changed

+187
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.user
1010
*.userosscache
1111
*.sln.docstates
12+
*.idea
1213

1314
# User-specific files (MonoDevelop/Xamarin Studio)
1415
*.userprefs
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@HostAddress = http://localhost:8080
2+
3+
POST {{HostAddress}}/add
4+
Content-Type: application/json
5+
6+
{"X":"1", "Y":"4"}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Xunit.Microsoft.DependencyInjection.ExampleTests.CalculationService;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
var app = builder.Build();
6+
7+
app.MapPost("add", (AddRequest request) => Results.Json(request.X + request.Y));
8+
9+
app.Run();
10+
11+
// Used for type reference in web application factory
12+
public partial class Program;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"applicationUrl": "http://localhost:8080",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
}
12+
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.CalculationService;
2+
3+
public record AddRequest(int X, int Y);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<IsPackable>false</IsPackable>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.Extensions.Options;
2+
using Options = Xunit.Microsoft.DependencyInjection.ExampleTests.Services.Options;
3+
4+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
5+
6+
public class CalculatorTests : TestBed<CalculatorFixture>
7+
{
8+
private readonly Options _options;
9+
10+
public CalculatorTests(ITestOutputHelper testOutputHelper, CalculatorFixture fixture)
11+
: base(testOutputHelper, fixture) => _options = _fixture.GetService<IOptions<Options>>(_testOutputHelper)!.Value;
12+
13+
[Theory]
14+
[InlineData(1, 2)]
15+
public async Task TestServiceAsync(int x, int y)
16+
{
17+
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper)!;
18+
var calculatedValue = await calculator.AddAsync(x, y);
19+
var expected = _options.Rate * (x + y);
20+
Assert.True(expected == calculatedValue);
21+
}
22+
23+
[Theory]
24+
[InlineData(1, 2)]
25+
public async Task TestScopedServiceAsync(int x, int y)
26+
{
27+
var calculator = _fixture.GetScopedService<ICalculator>(_testOutputHelper)!;
28+
var calculatedValue = await calculator.AddAsync(x, y);
29+
var expected = _options.Rate * (x + y);
30+
Assert.True(expected == calculatedValue);
31+
}
32+
}

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs renamed to examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/CalculatorFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures;
22

3-
public class TestFixture : TestBedFixture
3+
public class CalculatorFixture : TestBedFixture
44
{
55
protected override void AddServices(IServiceCollection services, IConfiguration? configuration)
66
=> services
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Mvc.Testing;
2+
3+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures;
4+
5+
public class RemoteCalculatorFixture : TestBedFixture
6+
{
7+
public WebApplicationFactory<Program> Factory { get; }
8+
9+
public RemoteCalculatorFixture(IMessageSink messageSink)
10+
{
11+
Factory = new WebApplicationFactory<Program>();
12+
}
13+
14+
protected override void AddServices(IServiceCollection services, IConfiguration? configuration)
15+
=> services
16+
.AddSingleton<IHttpClientFactory>(_ => new StaticClientFactory(Factory.CreateClient()))
17+
.AddTransient<ICalculator, RemoteCalculator>()
18+
.Configure<Options>(config => configuration?.GetSection("Options").Bind(config));
19+
20+
protected override ValueTask DisposeAsyncCore()
21+
=> new();
22+
23+
protected override IEnumerable<TestAppSettings> GetTestAppSettings()
24+
{
25+
yield return new() { Filename = "appsettings.json", IsOptional = false };
26+
}
27+
}
28+
29+
public class StaticClientFactory(HttpClient client) : IHttpClientFactory
30+
{
31+
public HttpClient CreateClient(string name)
32+
{
33+
return client;
34+
}
35+
}

0 commit comments

Comments
 (0)