0

コントローラーに、セッションの有効期限が切れているかどうか(== null)を確認するアクションがあり、有効期限が切れている場合は、ログインにリダイレクトします。このための単体テストを追加したいのですが、セッションを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());
    }

しかし、何も機能していません...

ありがとうございました!

4

1 に答える 1

0

これが私がそれをした方法です:

[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());
    }
于 2011-01-17T15:06:56.290 に答える