diff --git a/tests/Wax.IntegrationTests/Customers/CustomerTests.cs b/tests/Wax.IntegrationTests/Customers/CustomerTests.cs index d44aaeb..4d5d7ca 100644 --- a/tests/Wax.IntegrationTests/Customers/CustomerTests.cs +++ b/tests/Wax.IntegrationTests/Customers/CustomerTests.cs @@ -9,8 +9,12 @@ namespace Wax.IntegrationTests.Customers; -public class CustomerTests : TestBaseFixture +public class CustomerTests : IntegrationTestBase { + public CustomerTests(IntegrationFixture fixture) : base(fixture) + { + } + [Fact] public async Task ShouldCreateNewCustomer() { diff --git a/tests/Wax.IntegrationTests/IntegrationFixture.cs b/tests/Wax.IntegrationTests/IntegrationFixture.cs new file mode 100644 index 0000000..494e4ef --- /dev/null +++ b/tests/Wax.IntegrationTests/IntegrationFixture.cs @@ -0,0 +1,45 @@ +using System; +using Autofac; +using NSubstitute; +using Serilog; +using Wax.Core; +using Wax.Core.Data; +using Xunit; + +namespace Wax.IntegrationTests; + +public class IntegrationFixture : IDisposable, ICollectionFixture +{ + public readonly ILifetimeScope LifetimeScope; + + public IntegrationFixture() + { + var containerBuilder = new ContainerBuilder(); + var logger = Substitute.For(); + + containerBuilder.RegisterModule(new ApplicationModule(logger, new IntegrationTestUser(), "", + typeof(IntegrationFixture).Assembly)); + + LifetimeScope = containerBuilder.Build(); + } + + public void Cleanup() + { + var dbContext = LifetimeScope.Resolve(); + dbContext.Database.EnsureDeleted(); + } + + public void Dispose() + { + var dbContext = LifetimeScope.Resolve(); + dbContext.Database.EnsureDeleted(); + } +} + +[CollectionDefinition("Sequential")] +public class DatabaseCollection : ICollectionFixture +{ + // This class has no code, and is never created. Its purpose is simply + // to be the place to apply [CollectionDefinition] and all the + // ICollectionFixture<> interfaces. +} \ No newline at end of file diff --git a/tests/Wax.IntegrationTests/IntegrationTestBase.cs b/tests/Wax.IntegrationTests/IntegrationTestBase.cs new file mode 100644 index 0000000..e695ee7 --- /dev/null +++ b/tests/Wax.IntegrationTests/IntegrationTestBase.cs @@ -0,0 +1,52 @@ +using System; +using System.Threading.Tasks; +using Autofac; +using Wax.Core.Repositories; +using Xunit; + +namespace Wax.IntegrationTests; + +[Collection("Sequential")] +public class IntegrationTestBase : IDisposable +{ + private readonly IntegrationFixture _fixture; + + protected IntegrationTestBase(IntegrationFixture fixture) + { + _fixture = fixture; + } + + protected async Task Run(Func action, Action extraRegistration = null) + { + var dependency = extraRegistration != null + ? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve() + : _fixture.LifetimeScope.BeginLifetimeScope().Resolve(); + await action(dependency); + } + + protected async Task Run(Func> action, Action extraRegistration = null) + { + var dependency = extraRegistration != null + ? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve() + : _fixture.LifetimeScope.BeginLifetimeScope().Resolve(); + + return await action(dependency); + } + + protected async Task RunWithUnitOfWork(Func> action) + { + var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve(); + return await action(uow); + } + + protected async Task RunWithUnitOfWork(Func action) + { + var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve(); + await action(uow); + } + + public void Dispose() + { + _fixture.Cleanup(); + } +} \ No newline at end of file diff --git a/tests/Wax.IntegrationTests/IntegrationTestUser.cs b/tests/Wax.IntegrationTests/IntegrationTestUser.cs new file mode 100644 index 0000000..280c066 --- /dev/null +++ b/tests/Wax.IntegrationTests/IntegrationTestUser.cs @@ -0,0 +1,8 @@ +using Wax.Core.Services.Identity; + +namespace Wax.IntegrationTests; + +public class IntegrationTestUser : ICurrentUser +{ + public string Id => "_wax_test_user"; +} \ No newline at end of file diff --git a/tests/Wax.IntegrationTests/TestBaseFixture.cs b/tests/Wax.IntegrationTests/TestBaseFixture.cs deleted file mode 100644 index dd6b7f5..0000000 --- a/tests/Wax.IntegrationTests/TestBaseFixture.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Threading.Tasks; -using Autofac; -using NSubstitute; -using Serilog; -using Wax.Core; -using Wax.Core.Data; -using Wax.Core.Services.Identity; -using Xunit; - -namespace Wax.IntegrationTests; - -public class TestBaseFixture : IAsyncLifetime -{ - private readonly ILifetimeScope _scope; - - protected TestBaseFixture() - { - var containerBuilder = new ContainerBuilder(); - var logger = Substitute.For(); - - containerBuilder.RegisterModule(new ApplicationModule(logger, new IntegrationTestUser(), "", - typeof(TestBaseFixture).Assembly)); - - _scope = containerBuilder.Build(); - } - - protected async Task Run(Func action, Action extraRegistration = null) - { - var dependency = extraRegistration != null - ? _scope.BeginLifetimeScope(extraRegistration).Resolve() - : _scope.BeginLifetimeScope().Resolve(); - await action(dependency); - } - - protected async Task Run(Func> action, Action extraRegistration = null) - { - var dependency = extraRegistration != null - ? _scope.BeginLifetimeScope(extraRegistration).Resolve() - : _scope.BeginLifetimeScope().Resolve(); - - return await action(dependency); - } - - public Task InitializeAsync() - { - return Task.CompletedTask; - } - - public async Task DisposeAsync() - { - var dbContext = _scope.Resolve(); - - await dbContext.Database.EnsureDeletedAsync(); - } -} - -public class IntegrationTestUser : ICurrentUser -{ - public string Id => "__integration_test_user"; -} \ No newline at end of file