Abp testing

From burakdmir/abp-skills · View interactive version

Abp testing is an AI agent skill from the GitHub repository burakdmir/abp-skills. ABP Framework v10.x (10.4–10.6) testing quick reference: integration tests, *TestBase, SQLite in-memory, Shouldly, NSubstitute, data seeding, CurrentUser/CurrentTenant.Change. Use when you need to write tests in ABP.

How to use this skill

Skill file

ABP Framework — Testing

ABP v10.x testing. Integration tests are preferred: real services + SQLite in-memory DB, internal services not mocked.

Trigger

"ABP test", "integration test", "TestBase", "test data seed", "Shouldly", "NSubstitute".

Base Classes

| Project | Base Class |

|---|---|

| *.Domain.Tests | *DomainTestBase |

| *.Application.Tests | *ApplicationTestBase |

| *.EntityFrameworkCore.Tests | *EntityFrameworkCoreTestBase |

Test (AAA + Shouldly)

public class BookAppService_Tests : MyProjectApplicationTestBase
{
    private readonly IBookAppService _svc;
    public BookAppService_Tests() => _svc = GetRequiredService<IBookAppService>();

    [Fact]
    public async Task Should_Create_Book()
    {
        var result = await _svc.CreateAsync(new CreateBookDto { Name = "New", Price = 19.99m });
        result.Id.ShouldNotBe(Guid.Empty);
        result.Name.ShouldBe("New");
    }

    [Fact]
    public async Task Should_Throw_When_Invalid()
        => await Should.ThrowAsync<AbpValidationException>(async () =>
               await _svc.CreateAsync(new CreateBookDto { Name = "" }));
}

Naming: Should_ExpectedBehavior_When_Condition. Exception: ex.Code.ShouldBe("MyProject:ErrorCode").

Data Seed

public class TestDataSeedContributor : IDataSeedContributor, ITransientDependency
{
    public async Task SeedAsync(DataSeedContext context)
        => await _bookRepository.InsertAsync(new Book(TestBookId, "Test", 19.99m), autoSave: true);
}

Disable Authorization / Mock

context.Services.AddAlwaysAllowAuthorization();           // disable permissions

var emailSender = Substitute.For<IEmailSender>();          // NSubstitute (external only)
context.Services.AddSingleton(emailSender);

User / Tenant

using (CurrentUser.Change(TestData.UserId)) { /* ... */ }
using (CurrentTenant.Change(TestData.TenantId)) { /* ... */ }

Best Practices

  1. Integration tests + SQLite in-memory, don't mock internal services
  2. Keep each test independent, use meaningful seed data
  3. Cover edge cases + verify exception Code

Related

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.