使用 MvcContrib TestHelper 将会话设置为 Null
我的控制器中有一个操作,用于验证我的会话是否已过期(== 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是这样做的:
Here's how I did it :