【发布时间】:2017-07-07 05:05:33
【问题描述】:
我正在尝试创建一个集成测试项目,以使用此处的 Microsoft 示例最佳实践 https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing 测试我的 web api .Net Core 但我不知道为什么,我总是收到错误消息 [404 not found],HttpClient 找不到 'testServer' 。而且我确定我的“路线”是正确的 ["api/Registration/" + guid],如果我尝试从 iisexpress 调试模式。效果很好
如果有人有一些想法来解决它,将非常感激。谢谢
这是我在集成测试项目中使用的代码:
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);
}
这是我的解决方案 Visual Studio 的架构照片, 如您所见,集成测试项目与我的名为“注册”的 Web api 项目是分开的
【问题讨论】:
标签: .net unit-testing asp.net-core integration-testing httpclient