Skip to content

Commit

Permalink
test: add base class for armory infrastructure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Oct 20, 2021
1 parent d1e3889 commit 48354ac
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/Armory.Test/Armory.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.11"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="NUnit" Version="3.13.2"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0"/>
Expand Down
55 changes: 55 additions & 0 deletions test/Armory.Test/ArmoryContextInfrastructureTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Linq;
using Armory.Api;
using Armory.Shared.Helpers;
using Armory.Shared.Infrastructure.Persistence.EntityFramework;
using Armory.Shared.Test.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Armory.Test
{
public class ArmoryContextInfrastructureTestCase : InfrastructureTestCase<Startup>, IDisposable
{
public void Dispose()
{
Finish();
}

protected override void Setup()
{
}

protected override Action<IServiceCollection> Services()
{
return services =>
{
var descriptor =
services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ArmoryDbContext>));
if (descriptor is not null)
{
services.Remove(descriptor);
}
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
services.AddAutoMapper(AssemblyHelper.GetInstance(Assemblies.Armory));
services.AddMediatR(AssemblyHelper.GetInstance(Assemblies.Armory));
services.AddAutoMapper(typeof(Startup));
var serviceProvider = services.AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();
services.AddDbContext<ArmoryDbContext>(options =>
{
options.UseInMemoryDatabase(Guid.NewGuid().ToString());
options.UseInternalServiceProvider(serviceProvider);
});
};
}
}
}

0 comments on commit 48354ac

Please sign in to comment.