模拟 HttpContext(会话)
我读过很多关于 mvc 中模拟的文章和博客...其中许多很有帮助,但我仍然遇到一些问题:
其中一个问题是我需要在 My ActionResult 中使用 Session,但在我的测试中我访问 Session 时得到 NullReferenceException。
公共 ActionResult Index() { if (会话["某事"] == null) { Session.Add("某事",
); } 别的 { 会话["某事"] = ; } 返回重定向到操作(“Index2”); } 我的测试如下所示:
HomeController 控制器 = new HomeController; var result =controller.Index() as ViewResult; Assert.AreEqual("Index2", result.ViewName);
I've read many articles and blogs about mocking in mvc... Many of them were helpful, but i still have some problems:
One such issue is that I need to use Session in My ActionResult, but in my Tests i get a NullReferenceException when Session is accessed.
public ActionResult Index() { if (Session["Something"] == null) { Session.Add("Something", <smth>); } else { Session["Something"] = <smth>; } return redirect to action("Index2"); }
My test look like this:
HomeController controller = new HomeController; var result = controller.Index() as ViewResult; Assert.AreEqual("Index2", result.ViewName);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用诸如 MVC-contrib TestHelper 之类的工具。
该站点中的此示例演示了如何测试在会话中存储已发布表单值的操作
You can use tools such as the MVC-contrib TestHelper
This sample from the site shows how to test an action that stores a posted form value in the session