0

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);
}
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
Sam
  • 4,766
  • 11
  • 50
  • 76

1 Answers1

0

Considering this answer, I think that the Return part of your Setup method, should have the same signature of the method of interface:

mockAccountService.Setup(m => m.Login(It.Is<string>(userName => userName == "Samuel"),
                                          It.Is<string>(password => password == "password")))
                                          .Returns<bool>((u,p) => true);
Community
  • 1
  • 1
Augusto Pedraza
  • 538
  • 3
  • 18