使用 MvcContrib TestHelper 将会话设置为 Null

发布于 2024-10-06 11:01:53 字数 1394 浏览 6 评论 0原文

我的控制器中有一个操作,用于验证我的会话是否已过期(== null),如果是这样,则重定向到我的登录名。我想为此添加一个单元测试,但我无法将 Session 设置为 null 或模拟它。任何人都知道我该如何做到这一点以及测试它是否是个好主意?

这是我的控制器操作:

private InvestigationStep2Model _step2Model
    {
        get
        {
            if (Session == null) return null;
            if (Session["investigationStep2"] == null) Session["investigationStep2"] = new InvestigationStep2Model();
            return (InvestigationStep2Model) Session["investigationStep2"];
        }
        set { Session["investigationStep2"] = value; }
    }

public virtual ActionResult Step2()
    {
        if (_step2Model == null) return RedirectToAction(MVC.Session.Logout());
        ViewData.Model = _step2Model;
        return View();
    }

以及我对模拟会话的所有尝试的测试

[Test]
    public void Step2_RedirectToActionWhenNoSession()
    {
        _builder.InitializeController(_controller);

        Expect.Call(_controller.Session).Repeat.Any().Return(null);
        //_controller.HttpContext.Session.Abandon();//.SetSessionStateBehavior(SessionStateBehavior.Disabled); // .Session..Abandon());// .Stub(b => b.Session).Return(null);

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

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

,但没有任何效果......

谢谢!

I have an action in my controller that verify if my Session has expire ( == null) and, if it's the case, redirect to my login. I would like to add a unit test for this but I can't set the Session to null or either Mock it. Any one knows how I could do that and if it's a good idea to test it?

Here's my controller action :

private InvestigationStep2Model _step2Model
    {
        get
        {
            if (Session == null) return null;
            if (Session["investigationStep2"] == null) Session["investigationStep2"] = new InvestigationStep2Model();
            return (InvestigationStep2Model) Session["investigationStep2"];
        }
        set { Session["investigationStep2"] = value; }
    }

public virtual ActionResult Step2()
    {
        if (_step2Model == null) return RedirectToAction(MVC.Session.Logout());
        ViewData.Model = _step2Model;
        return View();
    }

And my test with all my attempts to mock Session

[Test]
    public void Step2_RedirectToActionWhenNoSession()
    {
        _builder.InitializeController(_controller);

        Expect.Call(_controller.Session).Repeat.Any().Return(null);
        //_controller.HttpContext.Session.Abandon();//.SetSessionStateBehavior(SessionStateBehavior.Disabled); // .Session..Abandon());// .Stub(b => b.Session).Return(null);

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

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

But nothing is working...

Thank you!

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

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

发布评论

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

评论(1

你与清晨阳光 2024-10-13 11:01:53

我是这样做的:

[Test]
    public void Step2_RedirectToActionWhenNoSession()
    {
        _builder.InitializeController(_controller);

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

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

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

Here's how I did it :

[Test]
    public void Step2_RedirectToActionWhenNoSession()
    {
        _builder.InitializeController(_controller);

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

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

        _mock.VerifyAll();
        result.AssertActionRedirect().ToAction<SessionController>(c => c.Logout());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文