Skip to content
bitzorcas
EN

Guide

编写新测试

如何为 BitzOrcas 编写测试 — 单元测试模式、集成测试模式和架构测试模式。

Last updated

选择正确的测试项目

你要测试…使用项目
领域逻辑 / 值对象BitzOrcas.Unit.Tests
Mediator 处理器 / 管道行为BitzOrcas.Application.Tests
带数据库的完整端点BitzOrcas.Integration.Tests
模块边界 / DI 规则BitzOrcas.Architecture.Tests

单元测试模式

public class NoteTests
{
[Fact]
public void Create_WithValidData_ShouldSucceed()
{
var note = new Note("Title", "Content");
note.Title.ShouldBe("Title");
note.DomainEvents.ShouldHaveSingleItem();
}
}

集成测试模式

public class NoteEndpointTests : IClassFixture<CustomWebApplicationFactory>
{
private readonly HttpClient _client;
public NoteEndpointTests(CustomWebApplicationFactory factory)
{
_client = factory.CreateClient();
}
[Fact]
public async Task GetNotes_Returns200()
{
var response = await _client.GetAsync("/api/notes");
response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
}

架构测试模式

[Fact]
public void Domain_ShouldNotDependOnInfrastructure()
{
var domain = Types.InAssembly(domainAssembly);
var infra = Types.InAssembly(infrastructureAssembly);
var result = domain.ShouldNot()
.HaveDependencyOnAny(infra.Types)
.GetResult();
result.IsSuccessful.ShouldBeTrue();
}