【问题标题】:SpecFlow Given and When Scope IssueSpecFlow 给定和范围问题时
【发布时间】: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


【解决方案1】:

设置_accountController 静态是否可以解决问题?还是像这样从头开始更新?

private AccountController _accountController = new AccountController();

【讨论】:

  • 是的,它确实保留了 AccountController。谢谢!
  • 我可以看到它是如何使它成为静态的,但这不是必需的。 specflow 将使用相同的实例,因此实例成员应保持不变。这种情况比问题中所涉及的更多。我使用该代码并没有看到问题,即使没有静态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多