Skip to content

Commit a6b010e

Browse files
authored
Merge pull request #10 from sj-distributor/test-enhance
Enhance integration tests
2 parents 2f20109 + 75d23f8 commit a6b010e

File tree

5 files changed

+110
-62
lines changed

5 files changed

+110
-62
lines changed

tests/Wax.IntegrationTests/Customers/CustomerTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
namespace Wax.IntegrationTests.Customers;
1111

12-
public class CustomerTests : TestBaseFixture
12+
public class CustomerTests : IntegrationTestBase
1313
{
14+
public CustomerTests(IntegrationFixture fixture) : base(fixture)
15+
{
16+
}
17+
1418
[Fact]
1519
public async Task ShouldCreateNewCustomer()
1620
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Autofac;
3+
using NSubstitute;
4+
using Serilog;
5+
using Wax.Core;
6+
using Wax.Core.Data;
7+
using Xunit;
8+
9+
namespace Wax.IntegrationTests;
10+
11+
public class IntegrationFixture : IDisposable, ICollectionFixture<IntegrationFixture>
12+
{
13+
public readonly ILifetimeScope LifetimeScope;
14+
15+
public IntegrationFixture()
16+
{
17+
var containerBuilder = new ContainerBuilder();
18+
var logger = Substitute.For<ILogger>();
19+
20+
containerBuilder.RegisterModule(new ApplicationModule(logger, new IntegrationTestUser(), "",
21+
typeof(IntegrationFixture).Assembly));
22+
23+
LifetimeScope = containerBuilder.Build();
24+
}
25+
26+
public void Cleanup()
27+
{
28+
var dbContext = LifetimeScope.Resolve<ApplicationDbContext>();
29+
dbContext.Database.EnsureDeleted();
30+
}
31+
32+
public void Dispose()
33+
{
34+
var dbContext = LifetimeScope.Resolve<ApplicationDbContext>();
35+
dbContext.Database.EnsureDeleted();
36+
}
37+
}
38+
39+
[CollectionDefinition("Sequential")]
40+
public class DatabaseCollection : ICollectionFixture<IntegrationFixture>
41+
{
42+
// This class has no code, and is never created. Its purpose is simply
43+
// to be the place to apply [CollectionDefinition] and all the
44+
// ICollectionFixture<> interfaces.
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Autofac;
4+
using Wax.Core.Repositories;
5+
using Xunit;
6+
7+
namespace Wax.IntegrationTests;
8+
9+
[Collection("Sequential")]
10+
public class IntegrationTestBase : IDisposable
11+
{
12+
private readonly IntegrationFixture _fixture;
13+
14+
protected IntegrationTestBase(IntegrationFixture fixture)
15+
{
16+
_fixture = fixture;
17+
}
18+
19+
protected async Task Run<T>(Func<T, Task> action, Action<ContainerBuilder> extraRegistration = null)
20+
{
21+
var dependency = extraRegistration != null
22+
? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve<T>()
23+
: _fixture.LifetimeScope.BeginLifetimeScope().Resolve<T>();
24+
await action(dependency);
25+
}
26+
27+
protected async Task<TR> Run<T, TR>(Func<T, Task<TR>> action, Action<ContainerBuilder> extraRegistration = null)
28+
{
29+
var dependency = extraRegistration != null
30+
? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve<T>()
31+
: _fixture.LifetimeScope.BeginLifetimeScope().Resolve<T>();
32+
33+
return await action(dependency);
34+
}
35+
36+
protected async Task<TR> RunWithUnitOfWork<TR>(Func<IUnitOfWork, Task<TR>> action)
37+
{
38+
var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve<IUnitOfWork>();
39+
return await action(uow);
40+
}
41+
42+
protected async Task RunWithUnitOfWork(Func<IUnitOfWork, Task> action)
43+
{
44+
var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve<IUnitOfWork>();
45+
await action(uow);
46+
}
47+
48+
public void Dispose()
49+
{
50+
_fixture.Cleanup();
51+
}
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Wax.Core.Services.Identity;
2+
3+
namespace Wax.IntegrationTests;
4+
5+
public class IntegrationTestUser : ICurrentUser
6+
{
7+
public string Id => "_wax_test_user";
8+
}

tests/Wax.IntegrationTests/TestBaseFixture.cs

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

0 commit comments

Comments
 (0)