问题描述
我正在尝试使用 NUnit 为我的 .NET 核心 API 创建集成测试项目。
我在尝试 API 调用时收到异常 404。
我为我的测试创建了 FakeStartup 类。如果我的启动,它只是一个副本。
注意:- 如果我通过了我的 IntegrationTestFactory 的 Startup 类,它可以正常工作,没有问题。
私有只读 IntegrationTestFactory factory = new();
public class IntegrationTestFactory<TTestStartup> : WebApplicationFactory<TTestStartup> where TTestStartup : class
{
protected override IHostBuilder CreateHostBuilder()
{
var host = Host.CreateDefaultBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<TTestStartup>();
webBuilder.UseEnvironment("Development");
})
.UseSerilog((hostingContext,loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration));
return host;
}
}
下面是我的测试课
[TestFixture]
public class SimpleIntegrationTests
{
private readonly IntegrationTestFactory<FakeStartup> factory = new();
[Test]
public async Task SimpleTestCase()
{
var client = factory.CreateClient();
// Arrange
var faker = new Faker();
var password = $"{faker.Internet.Password(prefix: "TestPassword12@")}$"; //to satisfy the requirement for passwords
var mockData = new Faker<SaveUserCommand>()
.RuleFor(u => u.FirstName,f => f.Name.LastName(Name.Gender.Male))
.RuleFor(u => u.LastName,f => f.Name.LastName(Name.Gender.Male))
.RuleFor(u => u.EmailAddress,(f,u) => f.Internet.Email(u.FirstName,u.LastName))
.RuleFor(u => u.MobileNumber,f => f.Phone.PhoneNumberFormat(10))
.RuleFor(u => u.Password,password)
.RuleFor(u => u.ConfirmPassword,password)
.RuleFor(u => u.IsFromregistrationPage,false)
.RuleFor(u => u.RoleType,f => f.PickRandom<RoleType>())
.RuleFor(u => u.PurposeType,f => f.PickRandom<PurposeType>());
var command = mockData.Generate();
var stringContent = new StringContent(command.ToJson(),Encoding.UTF8,"application/json");
var response = await client.PostAsync("/api/user",stringContent);
}
}
解决方法
如果我是对的(很久没有为 API 编写单元测试了)。是您仍然可以像正常测试一样在测试中添加控制器,因此测试看起来像这样。您可以将控制器用于您想要进行的呼叫类型。
[TestFixture]
public class SimpleIntegrationTests
{
[Test]
public async Task SimpleTestCase()
{
var controller = new yourControllerName(); //You can add the services it needs above (recommend mocking)
// Arrange
var faker = new Faker();
var password = $"{faker.Internet.Password(prefix: "TestPassword12@")}$"; //to satisfy the requirement for passwords
var mockData = new Faker<SaveUserCommand>()
.RuleFor(u => u.FirstName,f => f.Name.LastName(Name.Gender.Male))
.RuleFor(u => u.LastName,f => f.Name.LastName(Name.Gender.Male))
.RuleFor(u => u.EmailAddress,(f,u) => f.Internet.Email(u.FirstName,u.LastName))
.RuleFor(u => u.MobileNumber,f => f.Phone.PhoneNumberFormat(10))
.RuleFor(u => u.Password,password)
.RuleFor(u => u.ConfirmPassword,password)
.RuleFor(u => u.IsFromRegistrationPage,false)
.RuleFor(u => u.RoleType,f => f.PickRandom<RoleType>())
.RuleFor(u => u.PurposeType,f => f.PickRandom<PurposeType>());
var command = mockData.Generate();
var stringContent = new StringContent(command.ToJson(),Encoding.UTF8,"application/json");
var response = controller.Post(stringContent);
}
}
用记事本写的,这台笔记本电脑上没有编码程序。所以请告诉我它是否有效,或者您是否需要更多帮助。