我有一种情况来模拟Querystring。
是否有人用RhinoMocks嘲笑Querystring,如果是的话,请告诉我。我正在使用MVC 3。

谢谢

最佳答案

我找到了基于http://dylanbeattie.blogspot.com/2008/12/mocking-querystring-collection-in.html但使用RhinoMocks的解决方案

HttpContextBase httpContextBase;
HttpRequestBase httpRequestBase;
ControllerBase controllerBase;

controllerBase = mockRepository.DynamicMock<ControllerBase>();

NameValueCollection nvc = new NameValueCollection();
nvc.Add("KEY", "VALUE");

httpRequestBase  =  mockRepository.DynamicMock<HttpRequestBase>();
Expect.Call(httpRequestBase.QueryString).Return(nvc);

httpContextBase = mockRepository.DynamicMock<HttpContextBase>();
Expect.Call(httpContextBase.Request).Return(httpRequestBase);

var context = new ControllerContext(httpContextBase, new RouteData(), controllerBase);

yourController.ControllerContext = context;

09-03 19:57