我有一个模拟Querystring的情况。 有没有人用RhinoMocks嘲笑Querystring,如果有的话请让我知道。我使用MVC 3嘲笑Querystring-RhinoMocks-MVC3
谢谢
我有一个模拟Querystring的情况。 有没有人用RhinoMocks嘲笑Querystring,如果有的话请让我知道。我使用MVC 3嘲笑Querystring-RhinoMocks-MVC3
谢谢
我发现基于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;
这真的很酷...谢谢 –
虽然你问RhinoMocks,我发现这个解决方案,并适应它起订量。因此,对于任何感兴趣的人,这里是@ TomAx的答案的Moq版本:
NameValueCollection queryString = new NameValueCollection();
queryString.Add("KEY", "VALUE");
// Set up a request
var request = new Mock<HttpRequestBase>();
request.Setup(r => r.QueryString).Returns(queryString);
// Inject into the controller
var controllerBase = new Mock<ControllerBase>();
var contextBase = new Mock<HttpContextBase>();
contextBase.Setup(c => c.Request).Returns(request.Object);
request.Setup(r => r.QueryString).Returns(queryString);
var controllerContext = new ControllerContext(contextBase.Object, new RouteData(), controllerBase.Object);
var controller = new YourController();
controller.ControllerContext = controllerContext;
必须有一个更短的方式。 –
@ByteBlast如果你找到更短的方法,请在这里发布。 – ashes999
根据你的回答(谢谢)和其他一些人,我想出了这个帮手,他的身材略矮一些:https://gist.github.com/ByteBlast/8fbbb425bc60f582780e –
取决于你如何处理控制器中的查询字符串。你会发布你的控制器吗? –
http://stackoverflow.com/questions/677801/mocking-and-httpcontextbase-get-user –