RhinoMocks AssertWasCalled 抛出异常
RhinoMocks AssertWasCalled throws Exception
我是 TDD 和 RhinoMocks 的新手。
我正在尝试测试 AssertWasCalled 但遇到问题。我测试的构造函数如下:
public AccountControllerTests()
{
_webAuthenticator = MockRepository.GenerateMock<IWebAuthenticator>();
}
而我的测试是这样的:
[TestMethod]
public void AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials()
{
const string username = "good-username";
const string password = "good-password";
var model = new LoginModel { Username = username, Password = password };
_webAuthenticator.Stub(w => w.Authenticate(username, password)).Return(true);
var mockHttpContextBase = MockRepository.GenerateMock<HttpContextBase>();
var accountController = new AccountController(_webAuthenticator);
accountController.Login(model);
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(mockHttpContextBase, username));
}
我得到的错误是:
测试方法Paxium.Music.WebUI.Tests.Controllers.AccountControllerTests.AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials抛出异常:
Rhino.Mocks.Exceptions.ExpectationViolationException: IWebAuthenticator.CreateSignInTicket(Castle.Proxies.HttpContextBaseProxy7f274f09b6124e6da32d96dc6d3fface, "good-username");预期 #1,实际 #0。
我现在更改了我的代码如下 - 代码前后:
之前:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
public AccountController(IWebAuthenticator webAuthenticator)
{
_webAuthenticator = webAuthenticator;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(HttpContext, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
之后:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
private readonly HttpContextBase _contextBase;
public AccountController()
{
}
public AccountController(IWebAuthenticator webAuthenticator, HttpContextBase contextBase)
{
_webAuthenticator = webAuthenticator;
_contextBase = contextBase;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(_contextBase, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
我的测试现在通过了。当我的控制器用于真实时,我如何注入 contextBase?我正在使用 StructureMap。
您收到的错误消息表明 Assert 失败,即 webAuthenticator 对象 未 使用这些特定参数调用(因此预期为 #1,实际为 #0 异常消息) .
根据您提供的有限上下文,我怀疑您测试中的 HttpContextBase (mockHttpContextBase) 的 假实例与从您的服务器传递给 webAuthenticator 的对象不同生产代码。
有两种方法可以解决此问题:降低断言的严格程度或确保生产代码使用伪造的 http 上下文对象。如果您不关心在此测试中将哪个 HttpContext 实例传递给 webAuthenticator,您可以使用参数匹配器(Rhinomocks 称它们为 argument constraints)。
在你的情况下,结果会是这样的:
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(Arg<HttpContextBase>.Is.Anything, Arg<string>.Is.Equal(username)));
我是 TDD 和 RhinoMocks 的新手。
我正在尝试测试 AssertWasCalled 但遇到问题。我测试的构造函数如下:
public AccountControllerTests()
{
_webAuthenticator = MockRepository.GenerateMock<IWebAuthenticator>();
}
而我的测试是这样的:
[TestMethod]
public void AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials()
{
const string username = "good-username";
const string password = "good-password";
var model = new LoginModel { Username = username, Password = password };
_webAuthenticator.Stub(w => w.Authenticate(username, password)).Return(true);
var mockHttpContextBase = MockRepository.GenerateMock<HttpContextBase>();
var accountController = new AccountController(_webAuthenticator);
accountController.Login(model);
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(mockHttpContextBase, username));
}
我得到的错误是:
测试方法Paxium.Music.WebUI.Tests.Controllers.AccountControllerTests.AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials抛出异常: Rhino.Mocks.Exceptions.ExpectationViolationException: IWebAuthenticator.CreateSignInTicket(Castle.Proxies.HttpContextBaseProxy7f274f09b6124e6da32d96dc6d3fface, "good-username");预期 #1,实际 #0。
我现在更改了我的代码如下 - 代码前后:
之前:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
public AccountController(IWebAuthenticator webAuthenticator)
{
_webAuthenticator = webAuthenticator;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(HttpContext, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
之后:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
private readonly HttpContextBase _contextBase;
public AccountController()
{
}
public AccountController(IWebAuthenticator webAuthenticator, HttpContextBase contextBase)
{
_webAuthenticator = webAuthenticator;
_contextBase = contextBase;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(_contextBase, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
我的测试现在通过了。当我的控制器用于真实时,我如何注入 contextBase?我正在使用 StructureMap。
您收到的错误消息表明 Assert 失败,即 webAuthenticator 对象 未 使用这些特定参数调用(因此预期为 #1,实际为 #0 异常消息) .
根据您提供的有限上下文,我怀疑您测试中的 HttpContextBase (mockHttpContextBase) 的 假实例与从您的服务器传递给 webAuthenticator 的对象不同生产代码。
有两种方法可以解决此问题:降低断言的严格程度或确保生产代码使用伪造的 http 上下文对象。如果您不关心在此测试中将哪个 HttpContext 实例传递给 webAuthenticator,您可以使用参数匹配器(Rhinomocks 称它们为 argument constraints)。 在你的情况下,结果会是这样的:
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(Arg<HttpContextBase>.Is.Anything, Arg<string>.Is.Equal(username)));