使用单元测试项目中定义的启动类时,.NET Core TestServer返回404

时间:2018-11-22 21:33:51

标签: unit-testing asp.net-core integration-testing asp.net-core-2.1

我有一个基本的.net核心api网络应用和一个使用TestServer发出http请求的单元测试项目。

我有一个TestStartup类,该类将api项目中的Startup类子类化。

如果Startup类在单元测试项目中,则得到404响应。 如果将TestStartup类移至api项目,则会收到200条响应。

Api项目

Api.csproj

<PackageReference Include="Microsoft.AspNetCore.App" />

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

TestController.cs

public class TestController : ControllerBase
{
    [HttpGet("test")]
    public ObjectResult Get()
    {
        return Ok("data");
    }
}

单元测试项目

Tests.csproj

<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />

Tests.cs

[TestFixture]
public class Tests
{
    [Test]
    public async Task Test()
    {
        var server = new TestServer(WebHost.CreateDefaultBuilder()
            .UseStartup<TestStartup>()
            .UseEnvironment("Development"));

        var response = await server.CreateClient().GetAsync("test");
    }
}

Startup.cs

public class TestStartup : Startup
{ }

4 个答案:

答案 0 :(得分:4)

按照aspnetcore-2.2#test-app-prerequisities中的说明,在包含测试的项目文件中添加对Microsoft.AspNetCore.Mvc.Testing的引用,为我解决了该问题。

答案 1 :(得分:4)

如果有人遇到了针对ASP.NET Core 3.0 3.1 的定位,请不要忘记将测试项目SDK更改为Microsoft.NET.Sdk.Web,即< / p>

<Project Sdk="Microsoft.NET.Sdk.Web">

按照Test app prerequisites

答案 2 :(得分:3)

您可以尝试以下两种选择:

  • AddApplicationPart添加到Startup.cs

    namespace IntegrationTestMVC
    {
    public class Startup
    {         
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {   
           services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("IntegrationTestMVC"))); //"IntegrationTestMVC" is your original project name
        }
    
  • 尝试将TestFixture转换为IClassFixture

        public class IntegrationTestMVCUnitTest : IClassFixture<WebApplicationFactory<TestStartup>>
    {
        private readonly HttpClient _client;
        private readonly WebApplicationFactory<TestStartup> _factory;
    
        public IntegrationTestMVCUnitTest(WebApplicationFactory<TestStartup> factory)
        {
    
            _factory = factory;
            _client = factory.CreateClient();
        }
    
        [Fact]
        public async Task IndexRendersCorrectTitle()
        {
            var response = await _client.GetAsync(@"/test");
        }
    
    }
    

对于第二个选项,您可以参考Integration tests in ASP.NET Core

答案 3 :(得分:0)

我必须更新测试项目中的 nuget 包才能工作