Skip to content
This repository was archived by the owner on Jul 14, 2023. It is now read-only.

Using single scope per test improves performance #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion tests/Application.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
using NUnit.Framework;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Threading.Tasks;

namespace CleanTesting.Application.IntegrationTests
{
using static Testing;

[TestFixture]
public class TestBase
{
protected IServiceScope Scope { get; private set; }

[SetUp]
public async Task SetUp()
{
await ResetState();

this.Scope = CreateScope();
}

[TearDown]
public void BaseTearDown()
{
if (Scope != null)
{
Scope.Dispose();
Scope = null;
}
}

protected virtual async Task<TEntity> FindAsync<TEntity>(int id)
where TEntity : class
{
return await Testing.FindAsync<TEntity>(id, Scope);
}

protected virtual async Task AddAsync<TEntity>(TEntity entity)
where TEntity : class
{
await Testing.AddAsync<TEntity>(entity, Scope);
}

protected virtual async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
return await Testing.SendAsync<TResponse>(request, Scope);
}

protected virtual async Task<string> RunAsDefaultUserAsync()
{
return await Testing.RunAsDefaultUserAsync(Scope);
}
}
}
29 changes: 15 additions & 14 deletions tests/Application.IntegrationTests/Testing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ public void RunBeforeAnyTests()
services.Remove(currentUserServiceDescriptor);

// Register testing version
services.AddScoped(provider =>
Mock.Of<ICurrentUserService>(s => s.UserId == _currentUserId));
services.AddTransient<ICurrentUserService>(provider =>
{
var res = new Mock<ICurrentUserService>();
res.SetupGet(x => x.UserId).Returns(() => _currentUserId);
return res.Object;
});

_scopeFactory = services.BuildServiceProvider().GetService<IServiceScopeFactory>();

Expand All @@ -75,51 +79,48 @@ private void EnsureDatabase()
context.Database.Migrate();
}

public static IServiceScope CreateScope()
{
return _scopeFactory.CreateScope();
}

public static async Task ResetState()
{
await _checkpoint.Reset(_configuration.GetConnectionString("DefaultConnection"));

_currentUserId = null;
}

public static async Task<TEntity> FindAsync<TEntity>(int id)
public static async Task<TEntity> FindAsync<TEntity>(int id, IServiceScope scope)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();

var context = scope.ServiceProvider.GetService<ApplicationDbContext>();

return await context.FindAsync<TEntity>(id);
}

public static async Task AddAsync<TEntity>(TEntity entity)
public static async Task AddAsync<TEntity>(TEntity entity, IServiceScope scope)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();

var context = scope.ServiceProvider.GetService<ApplicationDbContext>();

context.Add(entity);

await context.SaveChangesAsync();
}

public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request, IServiceScope scope)
{
using var scope = _scopeFactory.CreateScope();

var mediator = scope.ServiceProvider.GetService<IMediator>();

return await mediator.Send(request);
}

public static async Task<string> RunAsDefaultUserAsync()
public static async Task<string> RunAsDefaultUserAsync(IServiceScope scope)
{
var userName = "test@local";
var password = "Testing1234!";

using var scope = _scopeFactory.CreateScope();

var userManager = scope.ServiceProvider.GetService<UserManager<ApplicationUser>>();

var user = new ApplicationUser { UserName = userName, Email = userName };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoItems.Commands
{
using static Testing;

public class CreateTodoItemTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoItems.Commands
{
using static Testing;

public class DeleteTodoItemTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoItems.Commands
{
using static Testing;

public class UpdateTodoItemDetailTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoItems.Commands
{
using static Testing;

public class UpdateTodoItemTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoLists.Commands
{
using static Testing;

public class CreateTodoListTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoLists.Commands
{
using static Testing;

public class DeleteTodoListTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoLists.Commands
{
using static Testing;

public class UpdateTodoListTests : TestBase
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace CleanTesting.Application.IntegrationTests.TodoLists.Queries
{
using static Testing;

public class GetTodosTests : TestBase
{
[Test]
Expand Down