Skip to content

Commit

Permalink
Merge pull request #10 from sj-distributor/test-enhance
Browse files Browse the repository at this point in the history
Enhance integration tests
  • Loading branch information
linwenda authored Apr 6, 2023
2 parents 2f20109 + 75d23f8 commit a6b010e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 62 deletions.
6 changes: 5 additions & 1 deletion tests/Wax.IntegrationTests/Customers/CustomerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Wax.IntegrationTests/IntegrationFixture.cs
Original file line number Diff line number Diff line change
@@ -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<IntegrationFixture>
{
public readonly ILifetimeScope LifetimeScope;

public IntegrationFixture()
{
var containerBuilder = new ContainerBuilder();
var logger = Substitute.For<ILogger>();

containerBuilder.RegisterModule(new ApplicationModule(logger, new IntegrationTestUser(), "",
typeof(IntegrationFixture).Assembly));

LifetimeScope = containerBuilder.Build();
}

public void Cleanup()
{
var dbContext = LifetimeScope.Resolve<ApplicationDbContext>();
dbContext.Database.EnsureDeleted();
}

public void Dispose()
{
var dbContext = LifetimeScope.Resolve<ApplicationDbContext>();
dbContext.Database.EnsureDeleted();
}
}

[CollectionDefinition("Sequential")]
public class DatabaseCollection : ICollectionFixture<IntegrationFixture>
{
// 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.
}
52 changes: 52 additions & 0 deletions tests/Wax.IntegrationTests/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
@@ -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<T>(Func<T, Task> action, Action<ContainerBuilder> extraRegistration = null)
{
var dependency = extraRegistration != null
? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve<T>()
: _fixture.LifetimeScope.BeginLifetimeScope().Resolve<T>();
await action(dependency);
}

protected async Task<TR> Run<T, TR>(Func<T, Task<TR>> action, Action<ContainerBuilder> extraRegistration = null)
{
var dependency = extraRegistration != null
? _fixture.LifetimeScope.BeginLifetimeScope(extraRegistration).Resolve<T>()
: _fixture.LifetimeScope.BeginLifetimeScope().Resolve<T>();

return await action(dependency);
}

protected async Task<TR> RunWithUnitOfWork<TR>(Func<IUnitOfWork, Task<TR>> action)
{
var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve<IUnitOfWork>();
return await action(uow);
}

protected async Task RunWithUnitOfWork(Func<IUnitOfWork, Task> action)
{
var uow = _fixture.LifetimeScope.BeginLifetimeScope().Resolve<IUnitOfWork>();
await action(uow);
}

public void Dispose()
{
_fixture.Cleanup();
}
}
8 changes: 8 additions & 0 deletions tests/Wax.IntegrationTests/IntegrationTestUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Wax.Core.Services.Identity;

namespace Wax.IntegrationTests;

public class IntegrationTestUser : ICurrentUser
{
public string Id => "_wax_test_user";
}
61 changes: 0 additions & 61 deletions tests/Wax.IntegrationTests/TestBaseFixture.cs

This file was deleted.

0 comments on commit a6b010e

Please sign in to comment.