Asp Net Core WebHostBuilder 在集成测试期间的奇怪行为
Asp Net Core WebHostBuilder strange behavior during integration testing
我在使用 Asp Net Core 制作的 Web 应用程序上工作,我尝试使用 TestServer 进行集成测试。
我按照这个 blog post 来设置我的
测试环境。
应用程序的 Startup.cs 如下所示:
public class Startup
{
public Startup(IHostingEnvironment env)
{
applicationPath = env.WebRootPath;
contentRootPath = env.ContentRootPath;
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(contentRootPath)
.AddJsonFile("appsettings.json")
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Many services are called here
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
// Many config are made here
loggerFactory.AddSerilog();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=auth}/{action=login}/{id?}");
});
}
}
对于集成测试,我使用此代码创建 WebHostBuilder
var builder = new WebHostBuilder()
.UseContentRoot(appRootPath)
.UseStartup<TStartup>()
.UseEnvironment("test")
.ConfigureServices(x =>
{
.AddWebEncoders();
});
如果我 运行 一个简单的测试来检查主页是否可以访问它就可以了。
出于某种原因,我必须在 Startup.xml 中更改一些配置。所以我在 WebHostBuilder 上的配置中添加了一个调用:
var builder = new WebHostBuilder()
.UseContentRoot(appRootPath)
.UseStartup<TStartup>()
.UseEnvironment("test")
.ConfigureServices(x =>
{
.AddWebEncoders();
})
.Configure(x => {
// Some specific configuration
});
而且,我不知道为什么(这就是为什么我需要你的帮助),当我像以前一样调试相同的简单测试时,
从未调用启动 class 的 ConfigureServices 和 Configure 方法...
即使我只是让 Configure 方法空白。
这种行为正常吗?
如何设置特定配置而不直接在 Startup.cs 中添加?
WebHostBuilder.Configure
replaces UseStartup
, they can't be used together. Instead you can register an IStartupFilter
inside ConfigureServices
. Look here.
我在使用 Asp Net Core 制作的 Web 应用程序上工作,我尝试使用 TestServer 进行集成测试。
我按照这个 blog post 来设置我的 测试环境。
应用程序的 Startup.cs 如下所示:
public class Startup
{
public Startup(IHostingEnvironment env)
{
applicationPath = env.WebRootPath;
contentRootPath = env.ContentRootPath;
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(contentRootPath)
.AddJsonFile("appsettings.json")
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Many services are called here
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
// Many config are made here
loggerFactory.AddSerilog();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=auth}/{action=login}/{id?}");
});
}
}
对于集成测试,我使用此代码创建 WebHostBuilder
var builder = new WebHostBuilder()
.UseContentRoot(appRootPath)
.UseStartup<TStartup>()
.UseEnvironment("test")
.ConfigureServices(x =>
{
.AddWebEncoders();
});
如果我 运行 一个简单的测试来检查主页是否可以访问它就可以了。
出于某种原因,我必须在 Startup.xml 中更改一些配置。所以我在 WebHostBuilder 上的配置中添加了一个调用:
var builder = new WebHostBuilder()
.UseContentRoot(appRootPath)
.UseStartup<TStartup>()
.UseEnvironment("test")
.ConfigureServices(x =>
{
.AddWebEncoders();
})
.Configure(x => {
// Some specific configuration
});
而且,我不知道为什么(这就是为什么我需要你的帮助),当我像以前一样调试相同的简单测试时, 从未调用启动 class 的 ConfigureServices 和 Configure 方法... 即使我只是让 Configure 方法空白。
这种行为正常吗?
如何设置特定配置而不直接在 Startup.cs 中添加?
WebHostBuilder.Configure
replaces UseStartup
, they can't be used together. Instead you can register an IStartupFilter
inside ConfigureServices
. Look here.