有没有人知道我为什么在尝试模拟我的帐户控制器登录方法时遇到此异常?
这是moq代码:
[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");
}
在我的AccountController / Login操作中,它在此行崩溃:
bool loginValid = _accountService.Login(userName, password);
并且使用_accountService
实例和模拟对象设置了IAccountService
。
public interface IAccountService
{
bool Login(string userName, string password);
}
答案 0 :(得分:0)
考虑this answer,我认为Setup方法的Return部分应该具有与接口方法相同的签名:
mockAccountService.Setup(m => m.Login(It.Is<string>(userName => userName == "Samuel"),
It.Is<string>(password => password == "password")))
.Returns<bool>((u,p) => true);