-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: use web application factory's DI service provider in tests
- Loading branch information
Showing
6 changed files
with
71 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 18 additions & 22 deletions
40
tests/CrowdParlay.Social.IntegrationTests/Tests/AuthorsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,41 @@ | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using CrowdParlay.Communication; | ||
using CrowdParlay.Social.Application.Abstractions; | ||
using CrowdParlay.Social.Application.DTOs; | ||
using CrowdParlay.Social.IntegrationTests.Fixtures; | ||
using FluentAssertions; | ||
using MassTransit.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace CrowdParlay.Social.IntegrationTests.Tests; | ||
|
||
public class AuthorsControllerTests : IClassFixture<WebApplicationContext> | ||
{ | ||
private readonly HttpClient _client; | ||
private readonly ITestHarness _harness; | ||
private readonly IServiceProvider _services; | ||
|
||
public AuthorsControllerTests(WebApplicationContext context) | ||
{ | ||
_client = context.Client; | ||
_harness = context.Harness; | ||
_services = context.Services; | ||
} | ||
|
||
[Fact(DisplayName = "User created event creates author")] | ||
public async Task CreateAuthor_Positive() | ||
[Fact(DisplayName = "Get user by ID returns user")] | ||
public async Task GetAuthorById_Positive() | ||
{ | ||
var @event = new UserCreatedEvent( | ||
UserId: Guid.NewGuid().ToString(), | ||
Username: "compartmental", | ||
DisplayName: "Степной ишак", | ||
AvatarUrl: null); | ||
// Arrange | ||
await using var scope = _services.CreateAsyncScope(); | ||
var authors = scope.ServiceProvider.GetRequiredService<IAuthorRepository>(); | ||
var author = await authors.CreateAsync( | ||
id: Guid.NewGuid(), | ||
username: "compartmental", | ||
displayName: "Степной ишак", | ||
avatarUrl: null); | ||
|
||
await _harness.Bus.Publish(@event); | ||
|
||
var message = await _client.GetAsync($"/api/v1/authors/{@event.UserId}"); | ||
// Act | ||
var message = await _client.GetAsync($"/api/v1/authors/{author.Id}"); | ||
message.StatusCode.Should().Be(HttpStatusCode.OK); | ||
|
||
// Assert | ||
var response = await message.Content.ReadFromJsonAsync<AuthorDto>(GlobalSerializerOptions.SnakeCase); | ||
response.Should().BeEquivalentTo(new AuthorDto | ||
{ | ||
Id = Guid.Parse(@event.UserId), | ||
Username = @event.Username, | ||
DisplayName = @event.DisplayName, | ||
AvatarUrl = @event.AvatarUrl | ||
}); | ||
response.Should().BeEquivalentTo(author); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
global using Xunit; | ||
global using Xunit; | ||
global using FluentAssertions; | ||
global using Authorization = CrowdParlay.Social.IntegrationTests.Services.Authorization; |