C# Repositories and Http responses not returning expected data using same dbContext XUnit - c#

I've been trying to implement integration tests with a database per test strategy, but I haven't been able to make it work as needed.
This is the factory class that uses WebApplicationFactory:
public class TestFactory<TProgram, TDbContext> : WebApplicationFactory<TProgram>
where TProgram : class where TDbContext : DbContext
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.RemoveDbContext<TDbContext>();
services.AddDbContext<TDbContext>(options =>
{
options.UseInMemoryDatabase(Guid.NewGuid().ToString());
});
services.EnsureDbCreated<TDbContext>();
});
}
}
This is the TestClass:
public class RolesControllerTest : IDisposable
{
private readonly TestFactory<Program, ADbContext> _factory;
private IServiceScope _scope;
private ADbContext_dbContext;
private readonly HttpClient _client;
private IRoleRepository _rolesRepository;
public RolesControllerTest()
{
_factory = new TestFactory<Program, ADbContext>();
_client = _factory.CreateClient();
_scope = _factory.Services.CreateScope();
var scopedServices = _scope.ServiceProvider;
_dbContext = scopedServices.GetRequiredService<ADbContext>();
_dbContext.Database.EnsureCreated();
}
public void Dispose()
{
_factory.Dispose();
_scope.Dispose();
}
// Tests ...
}
This is the test:
[Fact(DisplayName = "GetAsync returns a list of role models")]
public async Task GetAsync_ReturnsTaskOfRoleModelList()
{
var roleModelInDb = new RoleModel
{
Id = Guid.NewGuid(),
Name = "Role A",
Description = "Role A Description"
};
_rolesRepository = new RoleRepository(_dbContext, TestMapperHelper.GenerateTestMapper());
var roleModel = await _rolesRepository.CreateAsync(roleModelInDb);
var responseData = await _client.GetFromJsonAsync<List<RoleModel>>("/api/roles");
responseData.ShouldNotBeNull();
responseData.ShouldBeOfType<List<RoleModel>>();
responseData.Count.ShouldBe(1);
responseData[0].Id.ShouldBe(roleModel.Id);
responseData[0].Name.ShouldBe(roleModelInDb.Name);
}
The repository returns the expected data: the new roleModel that's been added to the db.
The responseData is a list as expected, but it's empty, so the test fails.
If I try to use a client instead of the repository to create the initial roleModel:
var createdResponse = await _client.PostAsJsonAsync("/api/roles", roleModelInDb);
var createdByClient = await TestResponseHelper.GetResponseContent<RoleModel>(createdResponse);
The createdResponse is a 200 OK Http response, and the role model createdByClient is a valid RoleModel, but the test fails, the list is still empty.
If I use a roleRepository to find the previously created roleModel by Id, the result is null.
If I'm using the same database context for the web factory and repositories, why is this happening?

Related

In integration test used not InMemoryDatabase, but i create test ApiFactory with InMemoryDatabase

I use ApiFactory for integration tests and mock DB. I took this code from the xUnit tests manual and use it in another project, where it work good.
public class ApiFactory: WebApplicationFactory<Program>
{
protected override IWebHostBuilder CreateWebHostBuilder()
{
return builder.ConfigureServices(services =>
{
services.AddDbContext<DatabaseContext>(options =>
options.UseInMemoryDatabase("TestDB"));
})
.UseStartup<Program>();
}
}
[Trait("Category", "Unit")]
public class UserTests : IClassFixture<ApiFactory>
{
private readonly WebApplicationFactory<Program> _factory;
public UserTests(ApiFactory factory)
{
_factory = factory;
}
[Fact]
public void Test1()
{
using (var scope = _factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using var db = scope.ServiceProvider.GetService<DatabaseContext>();
db.UserEntity.Add(New UserEntity(){Name = "Test"});
}
var result = _factory.CreateClient().GetAsync("api/v1/user");
var user = await result.ReadResponceContentAsync<UserEntity>();
Assert.Equal("Test", user.Name);
}
}
In this line
using (var scope = _factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
not creating ApiFactory, but filling db.
In this line
var user = await result.ReadResponceContentAsync<UserEntity>();
creating ApiFactory and invoke api, but db in service empty and test is fail.

Write integration test for grpc service

I use this repository to write an ASP.NET Core gRPC integration test. But if I have a service method that this call another gRPC service, I get an error that means the second service is not available.
My method code is something like this:
public async Task<GetPersonReply> GetPersonInfoAsync(GetPersonRequest request, CallContext context = default)
{
HttpContext httpContext = context.ServerCallContext.GetHttpContext();
LanguageExt.Option<string> userDisplayName = httpContext.User.Identity.Name;
GrpcChannel channel = Toolkit.ChannelFactory.CreateChannelWithCredentials("https://localhost:5201");
IAddressService client = channel.CreateGrpcService<IAddressService>();
GetAddressReply serviceReply = await client.GetAddressAsync(
new GetAddressRequest { Street = "test setree", ZipCode = "428" });
return new GetPersonReply
{
DisplayName = userDisplayName.Some(x => x).None(string.Empty),
Address = serviceReply.Address
};
}
My fixture class:
namespace IntegrationTests.Fixture
{
public sealed class TestServerFixture : IDisposable
{
private readonly WebApplicationFactory<Startup> _serverFactory;
private readonly WebApplicationFactory<SecondServer.Startup> _secondServerFactory;
public TestServerFixture()
{
_serverFactory = new WebApplicationFactory<Startup>();
_secondServerFactory = new WebApplicationFactory<SecondServer.Startup>();
HttpClient serverClient = _serverFactory.CreateDefaultClient(new ResponseVersionHandler());
HttpClient secondServerClient = _secondServerFactory.CreateDefaultClient(new ResponseVersionHandler());
ServerGrpcChannel = Toolkit.ChannelFactory.CreateChannelWithCredentials(
Contracts.GrpcUrlConstants.SERVER_GRPC_URL,
serverClient);
SecondServerGrpcChannel = Toolkit.ChannelFactory.CreateChannelWithCredentials(
Contracts.GrpcUrlConstants.SECOND_SERVER_GRPC_URL,
serverTestClient);
}
public GrpcChannel ServerGrpcChannel { get; }
public GrpcChannel SecondServerGrpcChannel { get; }
public void Dispose()
{
_serverFactory.Dispose();
_serverTestFactory.Dispose();
}
private class ResponseVersionHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
response.Version = request.Version;
return response;
}
}
}
}
And my test code :
namespace IntegrationTests
{
[Collection(TestCollections.ApiIntegration)]
public class PersonServiceAcceptanceTests
{
public PersonServiceAcceptanceTests(TestServerFixture testServerFixture)
{
GrpcChannel serverChannel = testServerFixture.ServerGrpcChannel;
GrpcChannel secondServerChannel = testServerFixture.TestServerGrpcChannel;
_clientPersonService = serverChannel.CreateGrpcService<IPersonService>();
_clientAddressService = testServerChannel.CreateGrpcService<IAddressService>();
}
private readonly IPersonService _clientPersonService;
private readonly IAddressService _clientAddressService;
[Theory]
[InlineData("test1", "987")]
[InlineData("test2", "123")]
public async Task GetAddressService_ShouludCall_Success(string street, string zipCode) --> this test pass successfully
{
GetAddressRequest request = new GetAddressRequest { Street = street, ZipCode = zipCode };
GetAddressReply result = await _clientAddressService.GetAddressAsync(request, CallContext.Default);
result.Should().NotBeNull();
result.Address.Should().NotBeNullOrWhiteSpace();
result.Address.Should().Contain(street);
result.Address.Should().Contain(zipCode);
}
[Fact]
public async Task GetPersonInfo_Should_Success() //My Issue -> this test has error and not pass
{
GetPersonRequest request = new GetPersonRequest { PersonId = "101" };
GetPersonReply result = await _clientPersonService.GetPersonInfoAsync(request, CallContext.Default);
result.Should().NotBeNull();
}
}
}
Is there anyone tell me how can I write an integration test containing two separate services that one call in another?

How to close Resharper test runner after finishing integration test in xUnit Test project (.NET Core)?

i am new to integration tests. I have an xUnit project in my solution which contains one test only.
Here's the definition of my test:
[Fact]
public async Task ShouldCreateUser()
{
// Arrange
var createUserRequest = new CreateUserRequest
{
Login = "testowyLogin",
Password = "testoweHaslo",
FirstName = "testoweImie",
LastName = "testoweNazwisko",
MailAddress = "test#test.pl"
};
var serializedCreateUserRequest = SerializeObject(createUserRequest);
// Act
var response = await HttpClient.PostAsync(ApiRoutes.CreateUserAsyncRoute,
serializedCreateUserRequest);
// Assert
response
.StatusCode
.Should()
.Be(HttpStatusCode.OK);
}
And the BaseIntegrationTest class definition:
public abstract class BaseIntegrationTest
{
private const string TestDatabaseName = "TestDatabase";
protected BaseIntegrationTest()
{
var appFactory = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
RemoveDatabaseContextFromServicesCollectionIfFound<EventStoreContext>(services);
RemoveDatabaseContextFromServicesCollectionIfFound<GrantContext>(services);
services
.AddDbContext<EventStoreContext>(options =>
options.UseInMemoryDatabase(TestDatabaseName))
.AddDbContext<GrantContext>(options =>
options.UseInMemoryDatabase(TestDatabaseName));
});
});
HttpClient = appFactory.CreateClient();
}
protected HttpClient HttpClient { get; }
protected static StringContent SerializeObject(object #object) =>
new StringContent(
JsonConvert.SerializeObject(#object),
Encoding.UTF8,
"application/json");
private static void RemoveDatabaseContextFromServicesCollectionIfFound<T>(IServiceCollection services)
where T : DbContext
{
var descriptor = services.SingleOrDefault(service =>
service.ServiceType == typeof(DbContextOptions<T>));
if (!(descriptor is null))
{
services
.Remove(descriptor);
}
}
}
When i run tests, it takes few seconds, and the test ends successfully. The problem is that Resharper Test Runner still runs, although i've already have collected results. what am i doing wrong here? Do i have to somehow dispose the HttpClient, after performing all tests? If so, how to achieve that? Thanks for any help.
It looks like you're actually booting the application inside the test rather than using the testhost (https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1)
public class BasicTests
: IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;
public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
{
_factory = factory;
}
[Theory]
[InlineData("/")]
[InlineData("/Index")]
[InlineData("/About")]
[InlineData("/Privacy")]
[InlineData("/Contact")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync(url);
// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
}
}
Notice the IClassFixture stuff.

Dependency Injection containers - how do I get a specific instance of an object?

For example, I have a Repository class for getting data from a database, and there are several service classes, let's say Service1, Service2, Service3.
I will have multiple Repository instances, for example, for two or three databases. And, you should be able to configure services to work with a specific database.
I can't figure out how to implement these dependencies using the Dependency Injection container.
As far as I understand, I can register the Repository service either as a Singleton, or a new instance will be created for each dependency.
But, I only need two repositories, Repository ("DB1") and Repository ("DB2"), and when creating a service instance, I should be able to choose which database to work with. That is, as an option-Service1(Repository ("DB1")), Service2 (Repository ("DB1")), Service1 (Repository ("DB2")).
For example:
public class Program
{
static void Main()
{
var connectionStringDb1 = "DB1 connection string";
var connectionStringDb2 = "DB2 connection string";
var repositoryDb1 = new Repository(connectionStringDb1);
var repositoryDb2 = new Repository(connectionStringDb2);
var smsSendService1 = new SmsSendService(repositoryDb1);
var smsSendService2 = new SmsSendService(repositoryDb2);
var emailSendService1 = new EmailSendService(repositoryDb1);
smsSendService1.Run();
var tasks = new Task[]
{
smsSendService1.Run(),
smsSendService2.Run(),
emailSendService1.Run()
};
Task.WaitAll(tasks);
}
}
public class Repository
{
private string _connectionString;
public Repository(string connectionString)
{
_connectionString = connectionString;
}
public object GetData()
{
// Getting data from the Database
var data = ...;
return data;
}
}
public class SmsSendService
{
private readonly Repository _repository;
public SmsSendService(Repository repository)
{
_repository = repository;
}
public Task Run()
{
return Task.Run(() =>
{
// Sending SMS in a loop
while (true)
{
var data = _repository.GetData();
// ...
Task.Delay(1000);
}
});
}
}
public class EmailSendService
{
private readonly Repository _repository;
public EmailSendService(Repository repository)
{
_repository = repository;
}
public Task Run()
{
return Task.Run(() =>
{
// Sending Email in a loop
while (true)
{
var data = _repository.GetData();
// ...
Task.Delay(1000);
}
});
}
}
Try to take a look at autofac named instances

Entity Framework mocking requires global context

I have recently began to dig into Entity Framework unit-testing with Entity Framework 6 mocking.
I have noticed the following thing:
Entity Framework mocking forces me to create a global context in my BL class, for example:
public class RefundRepayment : IDisposable
{
protected DbContext _dbContext = new DbContext();
/* more properties and class code */
public void Dispose()
{
_dbContext.Dispose();
}
}
I can't quite figure it out, as I'd rather implement the using statement in every method in order to deal with the DbContext, my code will look like:
public class RefundRepayment
{
/* more properties and class code */
public void AccessDb()
{
using(DbContext dbContext = new DbContext())
{
/* db code here */
}
}
}
Is there any specific reason why should we initialize a global context instead of implementing the using statement?
First off, you need to be using DI (via ninject, Unity, Core, etc) to pull this off.
Let me show you a simple sample of an EF GetAll() testing my MVC controller.
[Fact]
public void GetAllOk()
{
// Arrange
// Act
var result = _controller.GetAll() as OkObjectResult;
// Assert
Assert.NotNull(result);
var recordList = result.Value as List<DTO.Account>;
Assert.NotNull(recordList);
Assert.Equal(4, recordList.Count);
}
It relies on this startup code...
public class AccountsControllerTests
{
DatabaseFixture _fixture;
AccountsControllerV1 _controller;
public AccountsControllerTests(DatabaseFixture fixture)
{
_fixture = fixture;
_controller = new AccountsControllerV1(_fixture._uow);
}
What is DatabaseFixture? Glad you asked...
public class DatabaseFixture : IDisposable
{
public ApplicationDbContext _context;
public DbContextOptions<ApplicationDbContext> _options;
public IUoW _uow;
public DatabaseFixture()
{
var x = Directory.GetCurrentDirectory();
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.Tests.json", optional : true)
.Build();
_options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "ProviderTests")
.Options;
_context = new ApplicationDbContext(_options);
_context.Database.EnsureCreated();
Initialize();
_uow = new UoW(_context);
}
private void Initialize()
{
_context.Accounts.Add(new Entities.Account() { AccountNumber = "Number 1", AccountID = "", AccountUniqueID = "" });
_context.Accounts.Add(new Entities.Account() { AccountNumber = "Number 2", AccountID = "", AccountUniqueID = "" });
_context.Accounts.Add(new Entities.Account() { AccountNumber = "Number 3", AccountID = "", AccountUniqueID = "" });
_context.Accounts.Add(new Entities.Account() { AccountNumber = "Number 4", AccountID = "", AccountUniqueID = "" });
_context.SaveChanges();
}
public void Dispose()
{
// Clean Up
_context.Database.EnsureDeleted();
}
}
[CollectionDefinition("Database Collection")]
public class DatabaseCollection : ICollectionFixture<DatabaseFixture>
{
}
A few definitions used in the above code. I used a Unit of Work Pattern that contains references to all my EF repositories. I kept Entity (Database) classes and DTO (Data Transfer Object) Classes separate. I used an in-memory replacement for the EF database that I initialize at the beginning of each run and/or test so that my data is always known. I inject the Database Fixture into my test class (not each test) so I am not creating/destroying constantly. Then I create my controller passing in my database UoW definition.
You're real controller requires injection of the UoW container you've created with the real database. You are merely substituting a controlled database environment for your test.
public AccountsControllerV1(IUoW uow)
{
_uow = uow;
}
And yes, I use versioning for the sharp-eyed. And yes, this is a Core 2 example. Still applicable for EF 6, just need 3rd party DI ;)
And the controller method I am testing?
[HttpGet("accounts", Name ="GetAccounts")]
public IActionResult GetAll()
{
try
{
var recordList = _uow.Accounts.GetAll();
List<DTO.Account> results = new List<DTO.Account>();
if (recordList != null)
{
results = recordList.Select(r => Map(r)).ToList();
}
log.Info($"Providers: GetAccounts: Success: {results.Count} records returned");
return Ok(results);
}
catch (Exception ex)
{
log.Error($"Providers: GetAccounts: Failed: {ex.Message}");
return BadRequest($"Providers: GetAccounts: Failed: {ex.Message}");
}
}

Categories