在MVC RC1中使用Rhino Mocks存根HttpSessionState,可以通过以下步骤实现:
Install-Package Rhino.Mocks
var httpSessionState = MockRepository.GenerateStub<HttpSessionStateBase>();
Session["key"]
的get和set方法:httpSessionState.Stub(x => x["key"]).Return("value");
httpSessionState.Stub(x => x["key"] = "new value");
var controller = new MyController();
controller.ControllerContext = new ControllerContext(
new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()),
controller);
controller.ControllerContext.HttpContext.Stub(x => x.Session).Return(httpSessionState);
var result = controller.MyAction();
Assert.AreEqual("value", controller.ControllerContext.HttpContext.Session["key"]);
这样,就可以在MVC RC1中使用Rhino Mocks存根HttpSessionState了。
领取专属 10元无门槛券
手把手带您无忧上云