【发布时间】:2012-02-01 15:40:36
【问题描述】:
我正在执行以下使用 SpecFlow 框架编写的测试,当测试达到“Then”时,_accountController 为空。为什么?
[Binding]
public class RegisterUserSteps
{
private AccountController _accountController;
private ActionResult _result;
[When(@"the user goes to the register user screen")]
public void WhenTheUserGoesToTheRegisterUserScreen()
{
Console.WriteLine("When");
_accountController = new AccountController();
_result = _accountController.Register();
}
[Then(@"the register user view should be displayed")]
public void ThenTheRegisterUserViewShouldBeDisplayed()
{
Console.WriteLine("Then");
Assert.AreEqual("Register", _accountController.ViewData["Title"]);
}
}
更新1:
[Binding]
public class RegisterUserSteps
{
private AccountController _accountController = new AccountController();
private ActionResult _result;
[When(@"the user goes to the register user screen")]
public void WhenTheUserGoesToTheRegisterUserScreen()
{
_result = _accountController.Register();
}
[Then(@"the register user view should be displayed")]
public void ThenTheRegisterUserViewShouldBeDisplayed()
{
Assert.AreEqual("Register", _accountController.ViewData["Title"]);
}
}
【问题讨论】:
-
乍一看我找不到任何问题...如果将
_accountController = new AccountController();移动到[Given()]方法中会发生什么? ` -
由于某种原因,ThenTheRegisterUserViewShouldBeDisplayed 方法中的 _accountController 为空。这个故事没有给定的方法。
-
我知道没有 Given,但是如果您添加 Given 并将
AccountController的更新移动到它,奇怪的行为会消失吗?
标签: asp.net-mvc specflow