asp.net-mvc-3 - 模拟查询字符串-Rhino Mocks-MVC3

原文 标签 asp.net-mvc-3

Mocking Querystring-RhinoMocks-MVC3

I have a situation to Mock Querystring. Does anyone mocked Querystring with RhinoMocks, if so please let me know. I am using MVC 3.

Thank you

Answer

I have found a solution based on http://dylanbeattie.blogspot.com/2008/12/mocking-querystring-collection-in.html but using 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;

翻译

我有一种情况来模拟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;
相关推荐

asp.net-mvc-3 - 在所有Razor视图中导入命名空间

ajax - 根据从父级下拉列表中选择的内容填充子级下拉列表

.net - 验证使用.Net MVC 3在ViewModel中更改密码的当前密码的最佳实践?

c# - 302重定向仍尝试运行原始请求

asp.net - 如何扩展/覆盖MVC html.LabelFor

c# - global.asax中需要UrlHelper

javascript - 通过Javascript返回值将值传递给Controller View MVC3 Razor

c# - Nhibernate连接池问题

asp.net - 如何在我的MVC3模型绑定中包括Telerik文件上传?

c# - 从Spark迁移到Razor