我想实现我的应用程序的集成测试从教程如下:https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testingASPNET核心集成测试返回404
public class CreditCardApplicationShould
{
[Fact]
public async Task RenderApplicationForm()
{
var builder = new WebHostBuilder()
.UseContentRoot(@"C:\Users\usuario\source\repos\CreditCardApp\CreditCardApp")
.UseEnvironment("Development")
.UseStartup<CreditCardApp.Startup>()
.UseApplicationInsights();
var server = new TestServer(builder);
var client = server.CreateClient();
var response = await client.GetAsync("/Apply/Index");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.Contains("New Credit Card Application", responseString);
}
}
然而,当我试图运行集成测试,它给了我出现以下错误:
"Message: System.InvalidOperationException : The view 'Index' was not found. The following locations were searched: /Views/Apply/Index.cshtml /Views/Shared/Index.cshtml"
将集成测试从MVC应用程序中分离出来似乎是一个常见问题。
这里太
public class Startup
{
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();
CurrentEnvironment = env;
}
public IConfiguration Configuration { get; }
private IHostingEnvironment CurrentEnvironment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddApplicationPart(typeof(ApplyController).GetTypeInfo().Assembly);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
,我发现了一些变通的startup.cs,上面写着包括在Startup.cs的AddApplicationPart,但它仍然没有工作。
我不确定它是否工作,因为我使用.NET Core 2.0。我很欣赏任何提示。
我认为你需要验证内容根路径 – Nkosi
@Nkosi它不工作太。正如你所看到的,我很难编码ContentRoot的路径,甚至没有成功... –
看看如何在这里设置路径https://stackoverflow.com/a/37844252/5233410 – Nkosi