| name | abp-testing |
| description | ABP testing patterns - integration tests over unit tests, GetRequiredService, IDataSeedContributor, Shouldly assertions, AddAlwaysAllowAuthorization, NSubstitute mocking, WithUnitOfWorkAsync. Use when writing or reviewing tests for application services, domain services, or repositories in ABP projects. |
ABP Testing Patterns
Docs: https://abp.io/docs/latest/testing
Test Project Structure
| Project | Purpose | Base Class |
|---|
*.Domain.Tests | Domain logic, entities, domain services | *DomainTestBase |
*.Application.Tests | Application services | *ApplicationTestBase |
*.EntityFrameworkCore.Tests | Repository implementations | *EntityFrameworkCoreTestBase |
Integration Test Approach
ABP recommends integration tests over unit tests:
- Tests run with real services and database (SQLite in-memory)
- No mocking of internal services
- Each test gets a fresh database instance
Application Service Test
public class BookAppService_Tests : MyProjectApplicationTestBase
{
private readonly IBookAppService _bookAppService;
public ()
{
_bookAppService = GetRequiredService<IBookAppService>();
}
[]
{
result = _bookAppService.GetListAsync(
PagedAndSortedResultRequestDto()
);
result.TotalCount.ShouldBeGreaterThan();
result.Items.ShouldContain(b => b.Name == );
}
[]
{
input = CreateBookDto
{
Name = ,
Price = m
};
result = _bookAppService.CreateAsync(input);
result.Id.ShouldNotBe(Guid.Empty);
result.Name.ShouldBe();
result.Price.ShouldBe(m);
}
[]
{
input = CreateBookDto
{
Name = ,
Price = m
};
Should.ThrowAsync<AbpValidationException>( () =>
{
_bookAppService.CreateAsync(input);
});
}
}