我正在構建一個新的應用程序,使用ASP.NET 5與MVC6並嘗試建立一個基於this描述的測試項目。asp.net 5 vnext集成測試和視圖
它正常工作的API調用(返回ObjectResults),但當我點擊返回視圖的響應,我得到404響應。
我使用該項目的Unter測試啓動類,代碼是一樣的教程:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseIISPlatformHandler();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
[Route("/")]
public class HomeController : Controller
{
[HttpGet]
[AllowAnonymous]
[Route("/")]
public IActionResult Index()
{
return View();
}
[HttpGet]
[AllowAnonymous]
[Route("/ok")]
public IActionResult Ok()
{
return new ObjectResult("OK");
}
}
[TestClass]
public class HomeTests
{
private readonly HttpClient _client;
public HomeTests()
{
var server = new TestServer(TestServer.CreateBuilder()
.UseStartup<Startup>());
_client = server.CreateClient();
}
[TestMethod]
public async Task Index_returns_page()
{
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
//Assert.Equals("OK", responseString);
}
[TestMethod]
public async Task Index_OK_returns_OK()
{
var response = await _client.GetAsync("/ok");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.AreEqual("OK", responseString);
}
}
當然,在瀏覽器中的一切工作正常。
您可以分享您在UseStartup <>中使用的'Startup'的代碼嗎? –
我已經添加了完整的啓動和測試類代碼 –
控制器是否正確?它在瀏覽器中工作嗎?你嘗試調試你的控制器嗎? – poke