mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-07-01 19:12:57 +00:00
Add gRPC integration tests
This commit is contained in:
parent
7941c682d8
commit
31681617d9
@ -3,6 +3,7 @@ using System.IO;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using CleanArchitecture.Infrastructure.Database;
|
using CleanArchitecture.Infrastructure.Database;
|
||||||
using CleanArchitecture.IntegrationTests.Infrastructure;
|
using CleanArchitecture.IntegrationTests.Infrastructure;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace CleanArchitecture.IntegrationTests.Fixtures;
|
namespace CleanArchitecture.IntegrationTests.Fixtures;
|
||||||
@ -14,16 +15,17 @@ public class TestFixtureBase
|
|||||||
var projectDir = Directory.GetCurrentDirectory();
|
var projectDir = Directory.GetCurrentDirectory();
|
||||||
var configPath = Path.Combine(projectDir, "appsettings.Integration.json");
|
var configPath = Path.Combine(projectDir, "appsettings.Integration.json");
|
||||||
|
|
||||||
var factory = new CleanArchitectureWebApplicationFactory(
|
Factory = new CleanArchitectureWebApplicationFactory(
|
||||||
SeedTestData,
|
SeedTestData,
|
||||||
RegisterCustomServicesHandler,
|
RegisterCustomServicesHandler,
|
||||||
configPath);
|
configPath);
|
||||||
|
|
||||||
ServerClient = factory.CreateClient();
|
ServerClient = Factory.CreateClient();
|
||||||
ServerClient.Timeout = TimeSpan.FromMinutes(5);
|
ServerClient.Timeout = TimeSpan.FromMinutes(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient ServerClient { get; }
|
public HttpClient ServerClient { get; }
|
||||||
|
protected WebApplicationFactory<Program> Factory { get; }
|
||||||
|
|
||||||
protected virtual void SeedTestData(ApplicationDbContext context)
|
protected virtual void SeedTestData(ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using CleanArchitecture.Domain.Entities;
|
||||||
|
using CleanArchitecture.Domain.Enums;
|
||||||
|
using CleanArchitecture.Infrastructure.Database;
|
||||||
|
using CleanArchitecture.IntegrationTests.Infrastructure;
|
||||||
|
using Grpc.Net.Client;
|
||||||
|
|
||||||
|
namespace CleanArchitecture.IntegrationTests.Fixtures.gRPC;
|
||||||
|
|
||||||
|
public sealed class GetUsersByIdsTestFixture : TestFixtureBase
|
||||||
|
{
|
||||||
|
public GrpcChannel GrpcChannel { get; }
|
||||||
|
public Guid CreatedUserId { get; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
public GetUsersByIdsTestFixture()
|
||||||
|
{
|
||||||
|
GrpcChannel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
|
||||||
|
{
|
||||||
|
HttpHandler = Factory.Server.CreateHandler()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void SeedTestData(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
base.SeedTestData(context);
|
||||||
|
|
||||||
|
var user = CreateUser();
|
||||||
|
|
||||||
|
context.Users.Add(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User CreateUser()
|
||||||
|
{
|
||||||
|
return new User(
|
||||||
|
CreatedUserId,
|
||||||
|
"user@user.de",
|
||||||
|
"User First Name",
|
||||||
|
"User Last Name",
|
||||||
|
"User Password",
|
||||||
|
UserRole.User);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,4 @@
|
|||||||
using System;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CleanArchitecture.IntegrationTests.Fixtures;
|
using CleanArchitecture.IntegrationTests.Fixtures;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CleanArchitecture.IntegrationTests.Fixtures.gRPC;
|
||||||
|
using CleanArchitecture.Proto.Users;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CleanArchitecture.IntegrationTests.gRPC;
|
||||||
|
|
||||||
|
public sealed class GetUsersByIdsTests : IClassFixture<GetUsersByIdsTestFixture>
|
||||||
|
{
|
||||||
|
private readonly GetUsersByIdsTestFixture _fixture;
|
||||||
|
|
||||||
|
public GetUsersByIdsTests(GetUsersByIdsTestFixture fixture)
|
||||||
|
{
|
||||||
|
_fixture = fixture;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Should_Get_Users_By_Ids()
|
||||||
|
{
|
||||||
|
var client = new UsersApi.UsersApiClient(_fixture.GrpcChannel);
|
||||||
|
|
||||||
|
var request = new GetByIdsRequest();
|
||||||
|
request.Ids.Add(_fixture.CreatedUserId.ToString());
|
||||||
|
|
||||||
|
var response = await client.GetByIdsAsync(request);
|
||||||
|
|
||||||
|
response.Users.Count.Should().Be(1);
|
||||||
|
|
||||||
|
var user = response.Users.First();
|
||||||
|
var createdUser = _fixture.CreateUser();
|
||||||
|
|
||||||
|
user.Email.Should().Be(createdUser.Email);
|
||||||
|
user.FirstName.Should().Be(createdUser.FirstName);
|
||||||
|
user.LastName.Should().Be(createdUser.LastName);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user