首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RhinoMocks AssertWasCalled抛出异常

RhinoMocks AssertWasCalled抛出异常
EN

Stack Overflow用户
提问于 2015-05-26 07:12:18
回答 1查看 863关注 0票数 0

我是RhinoMocks和TDD的新手。

我正在试着测试AssertWasCalled,但是有问题。我测试的构造函数如下所示:

代码语言:javascript
复制
    public AccountControllerTests()
    {
        _webAuthenticator = MockRepository.GenerateMock<IWebAuthenticator>();
    }

我的测试是这样的:

代码语言:javascript
复制
    [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,“好用户名”);预期#1,实际#0。

现在,我已经将代码更改如下--在代码之前和之后:

在此之前:

代码语言:javascript
复制
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);
    }
}

之后:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-26 13:08:41

您正在接收的错误消息指示断言失败,即webAuthenticator对象是使用这些特定参数调用的not (因此预期#1,实际#0异常消息)。

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

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

代码语言:javascript
复制
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(Arg<HttpContextBase>.Is.Anything, Arg<string>.Is.Equal(username)));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30451828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档