From 2178d0fabd868fdab6a2117ac4d2e8a17e7f8693 Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Thu, 13 Oct 2022 08:51:43 +0300 Subject: [PATCH] added API tests --- .../Integration/Testing.cs | 7 ++- .../Integration/api/ControllerApiTest.cs | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 PracticeCalendar.UnitTests/Integration/api/ControllerApiTest.cs diff --git a/PracticeCalendar.UnitTests/Integration/Testing.cs b/PracticeCalendar.UnitTests/Integration/Testing.cs index e0da9e0..87ea062 100644 --- a/PracticeCalendar.UnitTests/Integration/Testing.cs +++ b/PracticeCalendar.UnitTests/Integration/Testing.cs @@ -26,11 +26,16 @@ namespace PracticeCalendar.UnitTests.Integration { cfg.AddSingleton(svc => domainEventServiceMock.Object); }); - + _scopeFactory = _factory.Services.GetRequiredService(); _configuration = _factory.Services.GetRequiredService(); } + public static HttpClient GetHttpClient() + { + return _factory.CreateClient(); + } + public static async Task SendAsync(IRequest request) { using var scope = _scopeFactory.CreateScope(); diff --git a/PracticeCalendar.UnitTests/Integration/api/ControllerApiTest.cs b/PracticeCalendar.UnitTests/Integration/api/ControllerApiTest.cs new file mode 100644 index 0000000..ce11d5e --- /dev/null +++ b/PracticeCalendar.UnitTests/Integration/api/ControllerApiTest.cs @@ -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); + } +} \ No newline at end of file