MVCContrib TestHelper 与 session.clear、session.abandon 和 Rhino Mock 相关的问题

发布于 2024-10-10 09:31:52 字数 3250 浏览 8 评论 0原文

您好,我正在尝试对控制器上的注销操作进行单元测试,但我很难在 HttpContext 中测试或存根我的会话。我正在使用 MVC Contrib TestHelper 来简化操作,但现在我需要一些帮助。

这是我的测试:

[TestFixture]
    public class SessionControllerTest
    {
        private ISession _session;
        private IConfigHelper _configHelper;
        private IAuthenticationService _authService;
        //private IMailHelper _mailHelper;
        private ICryptographer _crypto;
        private SessionController _controller;
        private TestControllerBuilder _builder;
        private MockRepository _mock;
    [SetUp]
    public void Setup()
    {
        _mock = new MockRepository();
        _session = _mock.DynamicMock<ISession>();
        _configHelper = _mock.DynamicMock<IConfigHelper>();
        _authService = _mock.DynamicMock<IAuthenticationService>();
        //_mailHelper = _mock.DynamicMock<IMailHelper>();
        _crypto = _mock.DynamicMock<ICryptographer>();
        _controller = new SessionController(_authService, _session, _crypto, _configHelper);
        _builder = new TestControllerBuilder();
        _builder.InitializeController(_controller);
    }
[Test]
        public void Logout_ReturnRedirectToAction()
        {
        _builder.InitializeController(_controller);

        _authService.SignOut();
        LastCall.Repeat.Once();
        _builder.Session["memberNumber"] = string.Empty;
        LastCall.Repeat.Once();
        _controller.Session.Clear();
        LastCall.Repeat.Any();
        _controller.Session.Abandon();
        LastCall.Repeat.Any();
        //_builder.Session.Stub(s => s.Clear());
        //_builder.Session.Stub(s => s.Abandon());
        //_builder.Session.Clear();
        //LastCall.Repeat.Once();
        //_builder.Session.Abandon();
        //LastCall.Repeat.Once();

        _mock.ReplayAll();
        var result = _controller.Logout();

        _mock.VerifyAll();
        result.AssertActionRedirect().ToAction<SessionController>(c => c.Login());
    }

你可以看到我的不同尝试。我收到一条错误消息,告诉我 Session.Abandon() 未实现,当您查看 MVCContrib 的 TestHelper 时,女巫是对的。但是我如何模拟或存根已经被 TestHelper 模拟的会话呢?

NUnit 中的异常:

System.NotImplementedException: 方法或操作不 实施的。在 MvcContrib.TestHelper.MockSession.Abandon()

感谢您的帮助!

编辑:这是新的工作测试

[Test]
    public void Logout_ReturnRedirectToAction()
    {
        _builder.InitializeController(_controller);
        var mockSession = _mock.Stub<HttpSessionStateBase>();

        _controller.HttpContext.BackToRecord();
        _controller.HttpContext.Stub(c => c.Session).Return(mockSession);
        _controller.HttpContext.Replay();

        _authService.SignOut();
        LastCall.Repeat.Once();
        _builder.Session["memberNumber"] = string.Empty;
        _controller.Session.Clear();
        LastCall.Repeat.Once();
        _controller.Session.Abandon();
        LastCall.Repeat.Once();

        _mock.ReplayAll();
        var result = _controller.Logout();

        _mock.VerifyAll();
        result.AssertActionRedirect().ToAction<SessionController>(c => c.Login());
    }

Hi I'm trying to unit test my logout action on my controller but I have hard times to test or stub my Session in the HttpContext. I'm using MVC Contrib TestHelper to make it easier but now I need a little help.

Here's my test :

[TestFixture]
    public class SessionControllerTest
    {
        private ISession _session;
        private IConfigHelper _configHelper;
        private IAuthenticationService _authService;
        //private IMailHelper _mailHelper;
        private ICryptographer _crypto;
        private SessionController _controller;
        private TestControllerBuilder _builder;
        private MockRepository _mock;
    [SetUp]
    public void Setup()
    {
        _mock = new MockRepository();
        _session = _mock.DynamicMock<ISession>();
        _configHelper = _mock.DynamicMock<IConfigHelper>();
        _authService = _mock.DynamicMock<IAuthenticationService>();
        //_mailHelper = _mock.DynamicMock<IMailHelper>();
        _crypto = _mock.DynamicMock<ICryptographer>();
        _controller = new SessionController(_authService, _session, _crypto, _configHelper);
        _builder = new TestControllerBuilder();
        _builder.InitializeController(_controller);
    }
[Test]
        public void Logout_ReturnRedirectToAction()
        {
        _builder.InitializeController(_controller);

        _authService.SignOut();
        LastCall.Repeat.Once();
        _builder.Session["memberNumber"] = string.Empty;
        LastCall.Repeat.Once();
        _controller.Session.Clear();
        LastCall.Repeat.Any();
        _controller.Session.Abandon();
        LastCall.Repeat.Any();
        //_builder.Session.Stub(s => s.Clear());
        //_builder.Session.Stub(s => s.Abandon());
        //_builder.Session.Clear();
        //LastCall.Repeat.Once();
        //_builder.Session.Abandon();
        //LastCall.Repeat.Once();

        _mock.ReplayAll();
        var result = _controller.Logout();

        _mock.VerifyAll();
        result.AssertActionRedirect().ToAction<SessionController>(c => c.Login());
    }

You can see my differents attemps. I get an error telling me that Session.Abandon() is not implemented, witch is right when you take a look at MVCContrib's TestHelper. But how can I mock or Stub the Session that's already mocked by the TestHelper?

The Exception in NUnit :

System.NotImplementedException : The
method or operation is not
implemented. at
MvcContrib.TestHelper.MockSession.Abandon()

Thank you for the help!

EDIT : Here's the new working test

[Test]
    public void Logout_ReturnRedirectToAction()
    {
        _builder.InitializeController(_controller);
        var mockSession = _mock.Stub<HttpSessionStateBase>();

        _controller.HttpContext.BackToRecord();
        _controller.HttpContext.Stub(c => c.Session).Return(mockSession);
        _controller.HttpContext.Replay();

        _authService.SignOut();
        LastCall.Repeat.Once();
        _builder.Session["memberNumber"] = string.Empty;
        _controller.Session.Clear();
        LastCall.Repeat.Once();
        _controller.Session.Abandon();
        LastCall.Repeat.Once();

        _mock.ReplayAll();
        var result = _controller.Logout();

        _mock.VerifyAll();
        result.AssertActionRedirect().ToAction<SessionController>(c => c.Login());
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-10-17 09:31:52

我已经有一段时间没有使用MvcContrib了,所以我拉下了最新的代码并做了一个快速测试项目。这很奇怪。查看 MvcContrib 代码(特别是 TestControllerBuilder),它为大多数对象(请求、响应、服务器等)创建模拟,但不为会话创建模拟。我不确定这是为什么——可能得问创作者。

但是,有一种方法可以自己嘲笑它。您可以创建自己的模拟会话,并告诉控制器使用您的模拟会话,而不是 MvcContrib.TestHelpers 中的模拟会话。这是我在测试中所做的:

var mockSession = MockRepository.GenerateStub<HttpSessionStateBase>();
controller.HttpContext.BackToRecord();
controller.HttpContext.Stub(c => c.Session).Return(mockSession);
controller.HttpContext.Replay();

现在我运行我的控制器方法,然后使用 Rhino.Mocks 的 AAA 语法来确保调用 Abandon 方法:

controller.Session.AssertWasCalled(s => s.Abandon());

如果您想使用记录/重放语义,您可以在调用 < 之前设置您的期望代码>controller.HttpContext.Replay()。

It's been a while since I used MvcContrib, so I pulled down the latest code and made a quick test project. It's very odd. Looking at the MvcContrib code (specifically, TestControllerBuilder), it creates mocks for most of the objects (request, response, server, etc...), but not for Session. I'm not sure why this is -- probably have to ask the creators.

However, there is a way to mock it yourself. You can create your own mock session and tell the controller to use yours instead of the one from MvcContrib.TestHelpers. Here's what I did in my test:

var mockSession = MockRepository.GenerateStub<HttpSessionStateBase>();
controller.HttpContext.BackToRecord();
controller.HttpContext.Stub(c => c.Session).Return(mockSession);
controller.HttpContext.Replay();

Now I run my controller method and then use Rhino.Mocks' AAA syntax for making sure the Abandon method was called:

controller.Session.AssertWasCalled(s => s.Abandon());

If you want to use record/replay semantics, you could set your expectations before calling controller.HttpContext.Replay().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文