Does anyone have any idea why I get this exception trying to mock my account controller login method?
Here is the moq code:
[TestMethod]
public void Can_Validate_User()
{
// Arrange
Mock<IAccountService> mockAccountService = new Mock<IAccountService>();
mockAccountService.Setup(m => m.Login(It.Is<string>(userName => userName == "Samuel"),
It.Is<string>(password => password == "password")))
.Returns<bool>(b => true);
AccountController target = new AccountController(mockAccountService.Object);
// Act
RedirectResult result1 = (RedirectResult)target.Login("Samuel", "password");
//RedirectResult result2 = (RedirectResult)target.Login("RK", "password");
//RedirectResult result3 = (RedirectResult)target.Login("Tatiana", "password");
// Assert
Assert.AreEqual(result1, "~/DashBoard/Cases");
//Assert.AreEqual(result2, "~/DashBoard/Cases");
//Assert.AreEqual(result3, "~/DashBoard/Cases");
}
In my AccountController/Login action it crashes on this line:
bool loginValid = _accountService.Login(userName, password);
And _accountService
is set with DI with a IAccountService
instance, a mock.object.
public interface IAccountService
{
bool Login(string userName, string password);
}