我正在开发一个 ASP.NET MVC 应用程序。其HttpApplication
看起来像这样:
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
//Do some sort of initialisation. Set the DependencyResolver.
}
}
对于集成测试,我想以编程方式启动应用程序并使用它的
DependencyResolver
与它互动。访问 DependencyResolver
看起来很简单,因为我可以调用DependencyResolver.Current
.但是,我还没有弄清楚如何让应用程序初始化逻辑运行。我试过调用
new MvcApplication().Init()
,但这似乎不会触发 Application_Start
方法调用。我尝试从 MvcApplication
继承并提供直接调用Application_Start
的权限,但是由于 ASP.NET MVC 的异常而失败。
最佳答案
您可以使用 Selenium WebDriver 进行 MVC 应用程序的集成测试。您可以通过编写 Web 浏览器要执行的操作以编程方式与应用程序 UI 交互。然后可以从 MSTest 运行集成测试,以便您可以针对 UI 编写断言。这是 Selenium 的文档 http://docs.seleniumhq.org/docs/03_webdriver.jsp
这是一个代码示例:
[TestMethod]
public void runUITest()
{
driver = new FirefoxDriver(); //launches a Firefox window so you can observe the test run
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 7));
driver.Navigate().GoToUrl("http://localhost/yourWebApp"));
//assert something
Assert.IsTrue(driver.FindElement(By.XPath("//fieldset[@id='YourControl']/div/div[2]/label/span/span")).Displayed);
//find an element and click it
driver.FindElement(By.Id("FormSubmitButton")).Click();
}
Selenium WebDriver 作为 NuGet 包提供。
关于.net - 如何以编程方式启动 ASP.NET MVC 应用程序并与之交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318415/