gpt4 book ai didi

.net - 如何以编程方式启动 ASP.NET MVC 应用程序并与之交互?

转载 作者:行者123 更新时间:2023-12-01 06:31:28 27 4
gpt4 key购买 nike

我正在开发一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com