我正在尝试使用.NET为我的NUnit核心API创建一个集成测试项目。
当我尝试API调用时,我得到了一个异常404。
我创建了用于测试的FakeStartup类。如果我的创业公司。
注意:-如果我通过了IntegrationTestFactory的启动课程,它就可以正常工作,没有问题。
私有只读IntegrationTestFactory工厂=新();
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);
}
}

发布于 2021-04-09 20:50:18
如果我是正确的(很长时间没有为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);
}
}这是用记事本写的,这台笔记本电脑上没有编码程序。所以让我知道它是否有效或者你是否需要更多的帮助。
https://stackoverflow.com/questions/67027920
复制相似问题