added API tests

This commit is contained in:
Claudiu Farcas
2022-10-13 08:51:43 +03:00
parent 924fc4e065
commit 2178d0fabd
2 changed files with 56 additions and 1 deletions
@@ -26,11 +26,16 @@ namespace PracticeCalendar.UnitTests.Integration
{
cfg.AddSingleton(svc => domainEventServiceMock.Object);
});
_scopeFactory = _factory.Services.GetRequiredService<IServiceScopeFactory>();
_configuration = _factory.Services.GetRequiredService<IConfiguration>();
}
public static HttpClient GetHttpClient()
{
return _factory.CreateClient();
}
public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
using var scope = _scopeFactory.CreateScope();
@@ -0,0 +1,50 @@
using System.Net;
using FluentAssertions;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.UnitTests.Integration;
using static PracticeCalendar.UnitTests.Integration.Testing;
public class ControllerApiTest : BaseTest
{
[Fact]
public async Task GetAllEventsEmptyTestAsync()
{
await RunBeforeAnyTests();
var http = GetHttpClient();
var httpResponse = await http.GetAsync("/api/events");
httpResponse.IsSuccessStatusCode.Should().BeTrue();
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
var responseStr = await httpResponse.Content.ReadAsStringAsync();
responseStr.Should().Be("[]");
}
[Fact]
public async Task GetAllEventsManyTestAsync()
{
await RunBeforeAnyTests();
await AddAsync(new PracticeEvent("Test Event", "Event description",
DateTime.Now.AddHours(-1), DateTime.Now.AddHours(1))
{
Id = 1,
Attendees = {
new Attendee("Claudiu F", "claudiuf@busybee.com")
{
Id = 1
}
}
});
var http = GetHttpClient();
var httpResponse = await http.GetAsync("/api/events");
httpResponse.IsSuccessStatusCode.Should().BeTrue();
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
var responseStr = await httpResponse.Content.ReadAsStringAsync();
responseStr.Length.Should().Be(257);
}
}