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
@@ -0,0 +1,74 @@
using FluentAssertions;
using Mapster;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Entities;
namespace PracticeCalendar.UnitTests.Application.Mapping
{
public class PracticeEventMappingTests
{
readonly string _eventTitle = "Event1";
readonly string _eventDescription = "Description";
readonly string _attendeeName = "Claudiu Farcas";
readonly string _attendeeEmail = "claudiu.farcas@testingbee.com";
readonly DateTime _eventStartTime = new DateTime(2022, 10, 10, 10, 25, 35);
readonly DateTime _eventEndTime = new DateTime(2022, 10, 12, 9, 15, 00);
public PracticeEventMappingTests()
{
TypeAdapterConfig.GlobalSettings.Default.MapToConstructor(true);
TypeAdapterConfig.GlobalSettings.NewConfig<PracticeEventDto, PracticeEvent>()
.ConstructUsing(src => new PracticeEvent(src.Title, src.Description, src.StartTime, src.EndTime));
}
[Fact]
public void PracticeEvent_to_PracticeEventDto()
{
var practiceEvent = new PracticeEvent(_eventTitle, _eventDescription, _eventStartTime, _eventEndTime);
var testAttendee = new Attendee(_attendeeName, _attendeeEmail);
practiceEvent.AddAttendee(testAttendee);
var dto = practiceEvent.Adapt<PracticeEventDto>();
dto.Should().NotBeNull();
dto.Title.Should().Be(_eventTitle);
dto.Description.Should().Be(_eventDescription);
dto.StartTime.Should().Be(_eventStartTime);
dto.EndTime.Should().Be(_eventEndTime);
dto.Attendees.Should().HaveCount(1);
dto.Attendees[0].Name.Should().Be(_attendeeName);
dto.Attendees[0].EmailAddress.Should().Be(_attendeeEmail);
dto.Attendees[0].IsAttending.Should().BeFalse();
}
[Fact]
public void PracticeEventDto_to_PracticeEvent()
{
var practiceEventDto = new PracticeEventDto
{
Title = _eventTitle,
Description = _eventDescription,
StartTime = _eventStartTime,
EndTime = _eventEndTime
};
var attendeeDto = new AttendeeDto
{
Name = _attendeeName,
EmailAddress = _attendeeEmail
};
practiceEventDto.Attendees.Add(attendeeDto);
var entity = practiceEventDto.Adapt<PracticeEvent>();
entity.Should().NotBeNull();
entity.Title.Should().Be(_eventTitle);
entity.Description.Should().Be(_eventDescription);
entity.StartTime.Should().Be(_eventStartTime);
entity.EndTime.Should().Be(_eventEndTime);
entity.Attendees.Should().HaveCount(1);
entity.Attendees[0].Name.Should().Be(_attendeeName);
entity.Attendees[0].EmailAddress.Should().Be(_attendeeEmail);
entity.Attendees[0].IsAttending.Should().BeFalse();
}
}
}
@@ -1,4 +1,5 @@
using FluentAssertions;
using FluentAssertions.Common;
using PracticeCalendar.Domain.Entities;
namespace PracticeCalendar.UnitTests.Domain
@@ -13,7 +14,7 @@ namespace PracticeCalendar.UnitTests.Domain
[Fact]
public void InitializeProperties()
{
var practiceEvent = new PracticeEvent(_eventTitle, _eventDescription);
var practiceEvent = new PracticeEvent(_eventTitle, _eventDescription, DateTime.Now, DateTime.Now);
practiceEvent.Title.Should().Be(_eventTitle);
practiceEvent.Description.Should().Be(_eventDescription);
practiceEvent.Attendees.Should().HaveCount(0);
@@ -24,25 +25,25 @@ namespace PracticeCalendar.UnitTests.Domain
{
Action act = () =>
{
var practiceEvent = new PracticeEvent(null!, _eventDescription);
var practiceEvent = new PracticeEvent(null!, _eventDescription, DateTime.Now, DateTime.Now);
};
act.Should().Throw<ArgumentNullException>();
act = () =>
{
var practiceEvent = new PracticeEvent(_eventTitle, null!);
var practiceEvent = new PracticeEvent(_eventTitle, null!, DateTime.Now, DateTime.Now);
};
act.Should().Throw<ArgumentNullException>();
act = () =>
{
var practiceEvent = new PracticeEvent(string.Empty, _eventDescription);
var practiceEvent = new PracticeEvent(string.Empty, _eventDescription, DateTime.Now, DateTime.Now);
};
act.Should().Throw<ArgumentException>();
act = () =>
{
var practiceEvent = new PracticeEvent(_eventTitle, string.Empty);
var practiceEvent = new PracticeEvent(_eventTitle, string.Empty, DateTime.Now, DateTime.Now);
};
act.Should().Throw<ArgumentException>();
}
@@ -50,7 +51,7 @@ namespace PracticeCalendar.UnitTests.Domain
[Fact]
public void AddAttendeeToEvent()
{
var practiceEvent = new PracticeEvent(_eventTitle, _eventDescription);
var practiceEvent = new PracticeEvent(_eventTitle, _eventDescription, DateTime.Now, DateTime.Now);
var testAttendee = new Attendee(_attendeeName, _atendeeEmail);
practiceEvent.AddAttendee(testAttendee);
practiceEvent.Attendees.Should().HaveCount(1);
@@ -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)
@@ -41,10 +41,6 @@
<ProjectReference Include="..\PracticeCalendar.Api\PracticeCalendar.API.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Application\" />
</ItemGroup>
<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>