首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET API核心5集成测试404

.NET API核心5集成测试404
EN

Stack Overflow用户
提问于 2021-04-09 20:33:21
回答 1查看 176关注 0票数 0

我正在尝试使用.NET为我的NUnit核心API创建一个集成测试项目。

当我尝试API调用时,我得到了一个异常404。

我创建了用于测试的FakeStartup类。如果我的创业公司。

注意:-如果我通过了IntegrationTestFactory的启动课程,它就可以正常工作,没有问题。

私有只读IntegrationTestFactory工厂=新();

代码语言:javascript
复制
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;
        }
    }

下面是我的考试课

代码语言:javascript
复制
[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);
    }
}

EN

回答 1

Stack Overflow用户

发布于 2021-04-09 20:50:18

如果我是正确的(很长时间没有为API编写单元测试)。您仍然可以像普通测试一样在测试中添加控制器,所以类似这样的测试看起来是这样的。您可以将控制器与要执行的调用类型一起使用。

代码语言:javascript
复制
[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);
    }
}

这是用记事本写的,这台笔记本电脑上没有编码程序。所以让我知道它是否有效或者你是否需要更多的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67027920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档