简体   繁体   English

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

[英]asp.net 5 vnext integration testing and views

I am building a new app using ASP.NET 5 with MVC6 and trying to set up a project with tests based on this description. 我正在使用带有MVC6的ASP.NET 5构建一个新的应用程序,并尝试根据描述建立一个带有测试的项目。

It is working fine for API calls (returning ObjectResults), but when I hit a response returning a view, I get 404 response. 对于API调用(返回ObjectResults)来说,它工作正常,但是当我点击返回视图的响应时,会得到404响应。

I am using startup class from the project unter test, and the code is the same as in tutorial: 我正在使用来自项目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);
    }
}

Of course in the browser everything works as expected. 当然,在浏览器中,一切都会按预期进行。

It can't find your views because the IApplicationEnvironment.ApplicationBasePath is set to the test project's directory. 它找不到您的视图,因为IApplicationEnvironment.ApplicationBasePath设置为测试项目的目录。 You should be replacing the IApplicationEnvironment implementation with your own that points at the project you're testing. 您应该用自己的IApplicationEnvironment实现替换它,该实现指向您正在测试的项目。 This will get a bit easier in RC2. 这将在RC2中变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM