首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在核心测试项目(.NET xUnit )中完成集成测试后,如何关闭Resharper测试运行器?

在核心测试项目(.NET xUnit )中完成集成测试后,如何关闭Resharper测试运行器?
EN

Stack Overflow用户
提问于 2020-10-06 23:20:30
回答 1查看 46关注 0票数 1

我对集成测试是个新手。我的解决方案中有一个xUnit项目,它只包含一个测试。下面是我的测试的定义:

代码语言:javascript
运行
复制
[Fact]
public async Task ShouldCreateUser()
{
    // Arrange
    var createUserRequest = new CreateUserRequest
    {
        Login = "testowyLogin",
        Password = "testoweHaslo",
        FirstName = "testoweImie",
        LastName = "testoweNazwisko",
        MailAddress = "test@test.pl"
    };
    var serializedCreateUserRequest = SerializeObject(createUserRequest);
    
    // Act
    var response = await HttpClient.PostAsync(ApiRoutes.CreateUserAsyncRoute,
        serializedCreateUserRequest);
    
    // Assert
    response
        .StatusCode
        .Should()
        .Be(HttpStatusCode.OK);
}

和BaseIntegrationTest类定义:

代码语言:javascript
运行
复制
public abstract class BaseIntegrationTest
{
    private const string TestDatabaseName = "TestDatabase";
    
    protected BaseIntegrationTest()
    {
        var appFactory = new WebApplicationFactory<Startup>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services =>
                {
                    RemoveDatabaseContextFromServicesCollectionIfFound<EventStoreContext>(services);
                    RemoveDatabaseContextFromServicesCollectionIfFound<GrantContext>(services);
                    
                    services
                        .AddDbContext<EventStoreContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName))
                        .AddDbContext<GrantContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName));
                });
            });
        
        HttpClient = appFactory.CreateClient();
    }

    protected HttpClient HttpClient { get; }
    
    protected static StringContent SerializeObject(object @object) =>
        new StringContent(
            JsonConvert.SerializeObject(@object),
            Encoding.UTF8,
            "application/json");

    private static void RemoveDatabaseContextFromServicesCollectionIfFound<T>(IServiceCollection services)
        where T : DbContext
    {
        var descriptor = services.SingleOrDefault(service =>
            service.ServiceType == typeof(DbContextOptions<T>));

        if (!(descriptor is null))
        {
            services
                .Remove(descriptor);
        }
    }
}

当我运行测试时,它需要几秒钟,并且测试成功结束。问题是Resharper Test Runner仍然在运行,尽管我已经收集了结果。我在这里做错了什么?在执行完所有测试后,我是否必须以某种方式处理HttpClient?如果是这样,如何实现呢?谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2020-10-07 04:00:29

看起来您实际上是在测试中引导应用程序,而不是使用testhost (https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1)

代码语言:javascript
运行
复制
public class BasicTests 
    : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;

    public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData("/")]
    [InlineData("/Index")]
    [InlineData("/About")]
    [InlineData("/Privacy")]
    [InlineData("/Contact")]
    public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync(url);

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8", 
            response.Content.Headers.ContentType.ToString());
    }
}

注意IClassFixture的东西。

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

https://stackoverflow.com/questions/64228851

复制
相关文章

相似问题

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