在Visual Studio 2015中使用量角器测试ASP.NET Core应用 [英] Testing ASP.NET Core app using Protractor in Visual Studio 2015
问题描述
我有一个基于核心模板包中angular2模板的ASP.NET核心项目.我添加了第二个带有xunit和量角器的项目对我的主应用程序运行单元测试.
I've got a ASP.NET Core Project based on the angular2 template from the core template pack. I added a second project with xunit and protractor to run unit test against my main app.
两个人都能很好地工作,我只想念一件事:
Both work nicely alone, there is just one small thing I miss:
当我要运行量角器测试时,我需要手动将主项目托管在某个地方.如果我不手动托管主项目,它将无法运行,因此所有测试都会失败,因为我的页面无法访问.
When I want to run the protractor tests I need to host the main project somewhere manually. If I don't host the main project manually it does not run, so all the Tests fail because my page is unreachable.
我可能需要从测试中以某种方式启动主应用程序/项目,但我不知道如何.量角器的所有示例都针对某些已经在运行的主页运行,而没有一个针对其他项目运行.
I probably need to start the main app / project somehow from the test, but I got no idea how. All examples for protractor run against some already running Homepages, none does run against some other project.
如何从测试项目中启动主ASP.NET Core项目,使其在运行以进行测试?
How do I start the main ASP.NET Core Project from my test Project, so it is running for testing?
奇怪的是:启动主应用程序一次后,将Strg + F5量角器与ChromeDriver一起使用会找到应用程序的主页.但是我需要手动运行一次才能使测试正常工作...
Weird thing is: after I started the main app once using Strg+F5 protractor with ChromeDriver will find the homepage of the app. But I need to run it once manually for the tests to work...
public class EndToEndTests : IDisposable
{
private const string Url = "https://localhost:44391/";
private readonly IWebDriver _driver;
private readonly NgWebDriver _browser;
public EndToEndTests()
{
// TODO: somehow get the ASP.NET Core project up and running
_driver = new ChromeDriver(@"\test\ChromeDriver");
_driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
_browser = new NgWebDriver(_driver);
}
public void Dispose()
{
_driver.Quit();
}
[Fact(DisplayName = "Has a title")]
public void HasATitle()
{
_browser.Url = Url;
Assert.Equal("My page", _browser.Title);
}
推荐答案
您需要在SetUp方法中自托管WebApp.
You need to self-host your WebApp in your SetUp method.
对于基于.NET 4.5 Owin的应用程序,您需要将Microsoft.Owin.Hosting包与WebApp.Start方法一起使用.
With .NET 4.5 Owin based applications, you need to use the Microsoft.Owin.Hosting package with WebApp.Start method.
使用.NET Core,您需要使用WebHostBuilder
With .NET Core, you need to use WebHostBuilder
这篇关于在Visual Studio 2015中使用量角器测试ASP.NET Core应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!