C# Asp.NETCore3.0 Api集成测试出现问题
我为Asp.NETCore3.0配置了AutoFac,但现在我在ControllerTestBase类上遇到了问题。我知道我不应该使用WebHostBuilder编写其他代码,但我尝试了很多次。我只有新的错误 ControllerTestBase.csC# Asp.NETCore3.0 Api集成测试出现问题,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,我为Asp.NETCore3.0配置了AutoFac,但现在我在ControllerTestBase类上遇到了问题。我知道我不应该使用WebHostBuilder编写其他代码,但我尝试了很多次。我只有新的错误 ControllerTestBase.cs namespace Passenger.Tests.EndToEnd.Controllers { public abstract class ControllerTestsBase { protected read
namespace Passenger.Tests.EndToEnd.Controllers
{
public abstract class ControllerTestsBase
{
protected readonly TestServer Server;
protected readonly HttpClient Client;
protected ControllerTestsBase()
{
var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
// Add TestServer
webHost.UseTestServer();
webHost.UseStartup<Startup>();
});
var host = hostBuilder.Start();
Client = host.GetTestClient();
}
protected static StringContent GetPayload(object data)
{
var json = JsonConvert.SerializeObject(data);
//Content-Type: "application/json"
return new StringContent(json, Encoding.UTF8, "application/json");
}
}
}
错误消息:您不需要使用Autofac,因为.net core 3.0提供内置依赖项注入 阅读更多关于 重构代码并使用依赖项注入进行对象初始化
namespace Passenger.Tests.EndToEnd.Controllers
{
public class ControllerTestsBase
{
private readonly ITestServer _testServer;
private readonly HttpClient Client;
protected ControllerTestsBase(ITestServer testServer,HttpClient client)
{
_testServer = testServer;
client.BaseAddress = new Uri("https://www.stackoverflow.com");
client.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");
client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample");
Client = client;
}
protected static StringContent GetPayload(object data)
{
var json = JsonConvert.SerializeObject(data);
//Content-Type: "application/json"
return new StringContent(json, Encoding.UTF8, "application/json");
}
}
}您遇到了什么错误?@MattU请看下面我的错误。您好,欢迎来到StackOverflow。正如Matt提到的,在问题中包含任何错误(包括它们的堆栈跟踪)是很重要的。我需要在课程中使用Autofac。
namespace Passenger.Tests.EndToEnd.Controllers
{
public class ControllerTestsBase
{
private readonly ITestServer _testServer;
private readonly HttpClient Client;
protected ControllerTestsBase(ITestServer testServer,HttpClient client)
{
_testServer = testServer;
client.BaseAddress = new Uri("https://www.stackoverflow.com");
client.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");
client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample");
Client = client;
}
protected static StringContent GetPayload(object data)
{
var json = JsonConvert.SerializeObject(data);
//Content-Type: "application/json"
return new StringContent(json, Encoding.UTF8, "application/json");
}
}