我有一个测试,我正在模拟在控制器中调用的 2 个服务调用。第一次调用(用户 POCO)的返回值用于调用第二次。问题是它似乎与第二个不匹配,因为它一直返回 null。如果我将服务更改为返回字符串而不是 User 对象,则它可以正常工作。
测试:
[Test]
public void GivenExternalReturnUrl_ReturnsHomePage()
{
var model = new Login
{
Username = "user1",
Password = "password1",
ReturnUrl = "http://google.com"
};
UserService.Setup(x => x.ValidateUser(model.Username, model.Password)).Returns(User);
var cookie = new HttpCookie("test");
UserService.Setup(x => x.BuildLoginCookie(User, model.RememberMe)).Returns(cookie);
var result = Controller.Login(model) as RedirectToRouteResult;
result.Should().Not.Be.Null();
result.RouteValues["controller"].Should().Equal("Home");
result.RouteValues["action"].Should().Equal("Index");
UserService.Verify(x => x.BuildLoginCookie(User, model.RememberMe), Times.Once());
}
BuildLoginCookie 返回 null 而不是 cookie 对象。
它正在测试的操作:
[HttpPost]
public ActionResult Login(Login model)
{
if (ModelState.IsValid)
{
var user = _userService.ValidateUser(model.Username, model.Password);
if (user != null)
{
var cookie = _userService.BuildLoginCookie(user, model.RememberMe);
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(model.ReturnUrl))
return Redirect(model.ReturnUrl);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("InvalidCredentials", "The user name or password provided is incorrect.");
}
// If we got this far, something failed, redisplay form
model.Password = null; //clear the password so they have to re-enter it
return View(model);
}
我需要做什么才能使 Moq 匹配第二次调用并返回我告诉它返回的值?