@@ -10,7 +10,15 @@ License](https://img.shields.io/github/license/ArwynFr/dotnet-integration-testin
1010
1111## Installation
1212
13- dotnet add package ArwynFr.IntegrationTesting
13+ ``` shell
14+ dotnet new classlib -n MyTestProject
15+ ```
16+
17+ ``` shell
18+ dotnet add package ArwynFr.IntegrationTesting
19+ dotnet add pacakge Microsoft.NET.Test.Sdk
20+ dotnet add package xunit.runner.visualstudio
21+ ```
1422
1523## Usage
1624
@@ -23,53 +31,100 @@ output, so you get them in the test output in case of failure. It also
2331overwrites the application configuration with values from user secrets
2432and environement variables.
2533
26- public class MyTest : IntegrationTestBase<Program>
34+ ``` cs
35+ public class MyTest : IntegrationTestBase <Program >
36+ {
37+ public MyTest (ITestOutputHelper output ) : base (output ) { }
38+
39+ [Fact ]
40+ public async Task OnTest ()
2741 {
28- public MyTest(ITestOutputHelper output) : base(output) { }
42+ // Call system under test
43+ var response = await Client .GetFromJsonAsync <OrderDto >($" /order" );
44+
45+ response .Should ().HaveValue ();
46+ }
47+
48+ // Override a service with fake implementation in the tested app
49+ protected override void ConfigureAppServices (IServiceCollection services )
50+ => services .AddSingleton <IMyService , FakeService >();
51+ }
52+ ```
53+
54+ ### EntityFrameworkCore integration
55+
56+ ``` cs
57+ public class TestBaseDb : IntegrationTestBase <Program , MyDbContext >
58+ {
59+ public TestBaseDb (ITestOutputHelper output ) : base (output ) { }
2960
30- [Fact]
31- public async Task OnTest()
32- {
33- // Call system under test
34- var response = await Client.GetFromJsonAsync<OrderDto>($"/order");
61+ [Fact ]
62+ public async Task OnTest ()
63+ {
64+ // Access the injected dbcontext
65+ var value = await Database .Values
66+ .Where (val => val .Id == 1 )
67+ .Select (val => val .Result )
68+ .FirstOrDefaultAsync ();
3569
36- response.Should().HaveValue();
37- }
70+ // Call system under test
71+ var result = await Client . GetFromJsonAsync < int >( " /api/value/1 " );
3872
39- // Override a service with fake implementation in the tested app
40- protected override void ConfigureAppServices(IServiceCollection services)
41- => services.AddSingleton<IMyService, FakeService>();
73+ result .Should ().Be (value + 1 );
4274 }
4375
44- ### EntityFrameworkCore integration
76+ // Create and drop a database for every test execution
77+ protected override IDatabaseTestStrategy <Context > DatabaseTestStrategy
78+ => IDatabaseTestStrategy <MyDbContext >.DatabasePerTest ;
79+
80+ // Configure EFcore with a random database name
81+ protected override void ConfigureDbContext (DbContextOptionsBuilder builder )
82+ => builder .UseSqlite ($" Data Source={Guid .NewGuid ()}.sqlite" );
83+ }
84+ ```
85+
86+ ### Fluent specification-based testing
87+
88+ ``` cs
89+ // Actual code redacted for brievty
90+ // Write a test driver that implements specifications:
91+ private class MySpecDriver (MyDbContext dbContext , HttpClient client ) : TestDriverBase <SpecDriver >
92+ {
93+ // Arranges
94+ public async Task ThereIsEntityWithName (string name ) { }
95+ public async Task ThereIsNoEntityWithName (string name ) { }
96+
97+ // Acts
98+ public async Task ListAllEntities () { }
99+ public async Task FindEntityWithName (string name ) { }
100+ public async Task CreateEntity (EntityDetails payload ) { }
101+
102+ // Asserts
103+ public async Task ResultShouldBe (string name ) { }
104+ public async Task DetailsCountShouldBe (int number ) { }
105+ }
106+
107+ public class MySpecTest (ITestOutputHelper output ) : TestBaseDb
108+ {
109+ // Write fluent specifiation test:
110+ [Theory , InlineData (" ArwynFr" )]
111+ public async Task OnTest (string name )
112+ {
113+ await Services .GetRequiredService <MySpecDriver >()
114+ .Given (x => x .ThereIsEntityWithName (name ))
115+ .When (x => x .FindEntityWithName (name ))
116+ .Then (x => x .ResultShouldBe (name ))
117+ .ExecuteAsync ();
118+ }
45119
46- public class TestBaseDb : IntegrationTestBase<Program, MyDbContext>
120+ // Configure DI library
121+ protected override void ConfigureAppServices (IServiceCollection services )
47122 {
48- public TestBaseDb(ITestOutputHelper output) : base(output) { }
49-
50- [Fact]
51- public async Task OnTest()
52- {
53- // Access the injected dbcontext
54- var value = await Database.Values
55- .Where(val => val.Id == 1)
56- .Select(val => val.Result)
57- .FirstOrDefaultAsync();
58-
59- // Call system under test
60- var result = await Client.GetFromJsonAsync<int>("/api/value/1");
61-
62- result.Should().Be(value + 1);
63- }
64-
65- // Create and drop a database for every test execution
66- protected override IDatabaseTestStrategy<Context> DatabaseTestStrategy
67- => IDatabaseTestStrategy<MyDbContext>.DatabasePerTest;
68-
69- // Configure EFcore with a random database name
70- protected override void ConfigureDbContext(DbContextOptionsBuilder builder)
71- => builder.UseSqlite($"Data Source={Guid.NewGuid()}.sqlite");
123+ base .ConfigureAppServices (services );
124+ services .AddSingleton (_ => new MySpecDriver (Database , Client ));
72125 }
126+ }
127+ ```
73128
74129## Contributing
75130
0 commit comments