test domain events (#2)

* test domain events

* cleanup
This commit is contained in:
2022-10-10 12:06:08 +03:00
committed by GitHub
parent 9076dcc542
commit 8d68f1b7a1
13 changed files with 140 additions and 20 deletions
@@ -3,12 +3,21 @@ using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Infrastructure.Persistence;
using PracticeCalendar.Infrastructure.Services;
namespace PracticeCalendar.UnitTests.Integration
{
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
private readonly Action<IServiceCollection> configServices;
public CustomWebApplicationFactory(Action<IServiceCollection> configServices)
{
this.configServices = configServices;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(configurationBuilder =>
@@ -27,7 +36,10 @@ namespace PracticeCalendar.UnitTests.Integration
services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("InMemoryDbForTesting")
);
configServices(services);
});
}
}
}
@@ -1,7 +1,8 @@
using FluentAssertions;
using Moq;
using PracticeCalendar.Application.PracticeEvents.Commands;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Common;
using static PracticeCalendar.UnitTests.Integration.Testing;
namespace PracticeCalendar.UnitTests.Integration.PracticeEvents
@@ -38,6 +39,9 @@ namespace PracticeCalendar.UnitTests.Integration.PracticeEvents
result.Should().NotBeNull();
result.Id.Should().NotBe(0);
result.Attendees.Count.Should().Be(2);
//check domain events count
domainEventServiceMock.Verify(x=>x.Publish(It.IsAny<DomainEventBase>()), Times.Exactly(2));
}
}
}
@@ -26,7 +26,8 @@ namespace PracticeCalendar.UnitTests.Integration.PracticeEvents
{
await RunBeforeAnyTests();
await AddAsync(new PracticeEvent("Test Event", "Event description")
await AddAsync(new PracticeEvent("Test Event", "Event description",
DateTime.Now.AddHours(-1), DateTime.Now.AddHours(1))
{
Id = 1,
Attendees = {
@@ -5,6 +5,8 @@ using Microsoft.Extensions.DependencyInjection;
using PracticeCalendar.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Common.Interfaces;
using Moq;
namespace PracticeCalendar.UnitTests.Integration
{
@@ -14,11 +16,19 @@ namespace PracticeCalendar.UnitTests.Integration
private static IConfiguration _configuration = null!;
private static IServiceScopeFactory _scopeFactory = null!;
public static Mock<IDomainEventService> domainEventServiceMock = null!;
public static async Task RunBeforeAnyTests()
{
_factory = new CustomWebApplicationFactory();
domainEventServiceMock = new Mock<IDomainEventService>();
_factory = new CustomWebApplicationFactory(cfg =>
{
cfg.AddSingleton(svc => domainEventServiceMock.Object);
});
_scopeFactory = _factory.Services.GetRequiredService<IServiceScopeFactory>();
_configuration = _factory.Services.GetRequiredService<IConfiguration>();
_configuration = _factory.Services.GetRequiredService<IConfiguration>();
}
public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)