-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from sj-distributor/test-enhance
Enhance integration tests
- Loading branch information
Showing
5 changed files
with
110 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file was deleted.
Oops, something went wrong.