0

我是 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 中注入?我正在使用结构图。

4

1 回答 1

1

您收到的错误消息表明 Assert 失败,即使用这些特定参数调用 webAuthenticator 对象(因此预期 #1,实际 #0 异常消息)。

从您提供的有限上下文中,我怀疑您测试中的 HttpContextBase (mockHttpContextBase) 假实例与从您的生产代码传递给 webAuthenticator 的对象不同。

有两种方法可以解决这个问题:使断言不那么严格或确保生产代码使用假的 http 上下文对象。如果您不关心在此测试中将哪个 HttpContext 实例传递给 webAuthenticator,您可以使用参数匹配器(Rhinomocks 将它们称为参数约束)。在你的情况下,结果会是这样的:

_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(Arg<HttpContextBase>.Is.Anything, Arg<string>.Is.Equal(username)));
于 2015-05-26T13:08:41.887 回答