从API继承的TestProject中包含的TestStartup.Startup(包含在单独的项目中)会导致404错误 [英] TestStartup contained in a TestProject that inherits from API.Startup (contained in separate project) causes 404 error
问题描述
我正在尝试将StartupTest.cs与XUnit和TestServer一起使用.
I am trying to use a StartupTest.cs with XUnit and TestServer.
我已使用Visual Studio 2017 15.9.3遵循以下说明:
I have followed these instructions using Visual Studio 2017 15.9.3:
1)创建一个新的解决方案.
1) Create a new solution.
2)将ASP.Net Core Web应用程序(Web API)添加到项目中.称之为API.没有Https或Docker.
2) Add an ASP.Net Core web application (web API) to the project. Call it API. No Https or Docker.
3)将xUnit测试项目(.NET Core)添加到解决方案.称之为:XUnitTestProject1
3) Add a xUnit Test project (.NET Core) to the solution. Call it: XUnitTestProject1
4)从XUnit项目中添加对Web API项目的引用.
4) Add a reference to the Web API project from the XUnit project.
5)在XUnit项目中安装以下Nuget软件包: Microsoft.AspNetCore.TestHost,Microsoft.Extensions.PlatformAbstractions和Microsoft.AspNetCore.All.
5) Install the following Nuget packages in the XUnit project: Microsoft.AspNetCore.TestHost, Microsoft.Extensions.PlatformAbstractions and Microsoft.AspNetCore.All.
6)将以下类添加到单元测试项目中(请注意,Startup是来自Web api项目的Startup.cs):
6) Add the following class to the Unit Test project (note that Startup is the Startup.cs from the web api project):
using API;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.PlatformAbstractions;
using System;
using System.IO;
using System.Net.Http;
namespace XUnitTestProject1
{
public class TestServerFixture : IDisposable
{
private readonly TestServer _testServer;
public HttpClient Client { get; }
public TestServerFixture()
{
var builder = new WebHostBuilder()
.UseContentRoot(GetContentRootPath())
.UseEnvironment("Test")
.UseStartup<Startup>(); // Uses Start up class from your API Host project to configure the test server
_testServer = new TestServer(builder);
Client = _testServer.CreateClient();
}
private string GetContentRootPath()
{
var testProjectPath = PlatformServices.Default.Application.ApplicationBasePath;
var relativePathToHostProject = @"..\..\..\..\..\..\TestHost";
return Path.Combine(testProjectPath, relativePathToHostProject);
}
public void Dispose()
{
Client.Dispose();
_testServer.Dispose();
}
}
}
8)将此测试添加到测试项目中:
8) Add this test to the test project:
using System;
using Xunit;
namespace XUnitTestProject1
{
public class UnitTest1
{
private readonly TestServerFixture _fixture;
public UnitTest1(TestServerFixture fixture)
{
_fixture = new TestServerFixture(); //Make sure class implements IClassFixture.
}
[Fact]
public async System.Threading.Tasks.Task Test1Async()
{
var response = await _fixture.Client.GetAsync("api/Values");
response.EnsureSuccessStatusCode();
var responseStrong = await response.Content.ReadAsStringAsync();
}
}
}
9)运行测试并收到成功的响应(200).到此为止一切都很好.
9) Run the test and receive a successful response (200). All is well up to this point.
10)下一步是在单元测试项目中创建一个TestStartup类:
10) The next step is to create a TestStartup class in the Unit Test project:
public class StartupTest : Startup
{
public StartupTest(IConfiguration env) : base(env)
{
}
}
注意:我意识到这个Startup类目前是毫无意义的-我确实打算在工作后重写一些方法.
Note: I realise this Startup class is pointless at the moment - I do plan to override some methods once I get it working.
11)修改第6点的代码,即更改此行:
11) Amend the code in point 6 i.e. change this line:
.UseStartup<Startup>();
收件人:
.UseStartup<StartupTest>();
12)再次运行测试,看到404响应而不是200响应.
12) Run the test again and see a 404 response instead of 200 response.
添加TestStartup类后,为什么会看到404响应.
Why do I see a 404 response after I add the TestStartup class.
TestStartup继承自API.Startup.
TestStartup inherits from API.Startup.
推荐答案
我在这里找到了答案: https://github.com/aspnet/Mvc/issues/5992 .我只需要重写StartupTest中的ConfigureServices即可,如下所示:
I found the answer here: https://github.com/aspnet/Mvc/issues/5992. I simply had to override ConfigureServices in StartupTest as follows:
public override void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddApplicationPart(typeof(ValuesController).Assembly);
}
这篇关于从API继承的TestProject中包含的TestStartup.Startup(包含在单独的项目中)会导致404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!