测试集成.Net Core与TestServer返回404找不到?
问题描述:
我试图创建一个集成测试项目,用于测试我的网络api .Net核心与微软样本这里的最佳做法https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing 但我不知道为什么,我总是得到一个错误消息[404找不到], HttpClient找不到'testServer'。而且我确定我的'路由'是正确的[“api/Registration /”+ guid],如果我尝试iisexpress的调试模式。它的工作很好测试集成.Net Core与TestServer返回404找不到?
如果有人有一些想法来解决它,将是非常gratefull。由于
这是我在整合测试项目中使用的代码:
RegistrationIntegrationTest.cs:
[TestClass]
public class RegistrationIntegrationTest
{
private static TestServer server;
private static HttpClient client;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var projectPath = Path.GetFullPath(Path.Combine(basePath, "../../../../Registration"));
var builder = new WebHostBuilder()
.UseContentRoot(projectPath)
.UseEnvironment(EnvironmentName.Development)
.UseStartup<Startup>();
server = new TestServer(builder);
client = server.CreateClient();
}
private async Task<string> GetRegistrationResponse(string guid)
{
var request = "api/Registration/" + guid;
var response = await client.GetAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
[TestMethod]
public async Task GetRegistration()
{
// Act
var responseString = await GetRegistrationResponse("A5A3CBFD-8B61-E711-80E2-00505693113A");
// Assert
Assert.Equals("test",
responseString);
}
}
这里是我的启动类从我的集成测试项目:
Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
//.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
//.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddCors();
services.AddAntiforgery();
services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(this.Container));
services.Configure<MyAppSettings>(Configuration);
services.UseSimpleInjectorAspNetRequestScoping(this.Container);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
if (env.IsDevelopment())
{
loggerFactory.AddDebug();
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}
else if (env.IsStaging())
{
app.UseCors(builder => builder.WithOrigins("https://apitest.swisscaution.ch"));
}
else if (env.IsProduction())
{
app.UseCors(builder => builder.WithOrigins("https://internalapi.swisscaution.ch"));
}
else
{
throw new InvalidOperationException("Bad application environement.");
}
app.UseMvc();
this.InitializeContainer(app);
}
答
我终于发现问题了。 是因为我的[集成测试项目]的目标是.Net核心版本,我试图用.Net Framework 4.5.6版本测试和托管一个项目。 我在网上找到解决方案,我需要修改csproj文件并在“PropertyGroup”部分添加这3个配置。
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>