asp.net 5 个 vnext 集成测试和视图

asp.net 5 vnext integration testing and views

我正在使用 ASP.NET 5 和 MVC6 构建一个新应用程序,并尝试根据 this 描述建立一个带有测试的项目。

API 调用(返回 ObjectResults)工作正常,但是当我点击返回视图的响应时,我收到 404 响应。

我正在使用测试项目中的启动class,代码与教程中的相同:

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);
    }
}

当然在浏览器中一切正常。

无法找到您的意见,因为IApplicationEnvironment.ApplicationBasePath 设置为测试项目的目录。您应该将 IApplicationEnvironment 实现替换为指向您正在测试的项目的您自己的实现。这在 RC2 中会变得更容易一些。