I am executing the following test written using SpecFlow framework and when the test reaches “Then” the _accountController is null. Why?
[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"]);
}
}
UPDATE 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"]);
}
}
Does making the
_accountControllerstatic resolve the issue? Or newing it up right from the beginning like this?