3 回答
TA贡献1898条经验 获得超8个赞
使用最小起订量看起来像这样:
var request = new Mock<HttpRequestBase>();
request.Expect(r => r.HttpMethod).Returns("GET");
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Expect(c => c.Request).Returns(request.Object);
var controllerContext = new ControllerContext(mockHttpContext.Object
, new RouteData(), new Mock<ControllerBase>().Object);
我认为Rhino Mocks的语法相似。
TA贡献1719条经验 获得超6个赞
这是Jason链接的摘录。与Phil的方法相同,但使用犀牛。
注意:在嘲笑了mockRequest的内部之前,将嘲笑mockHttpContext.Request 返回mockRequest 。我相信此命令是必需的。
// create a fake web context
var mockHttpContext = MockRepository.GenerateMock<HttpContextBase>();
var mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
mockHttpContext.Stub(x => x.Request).Return(mockRequest);
// tell the mock to return "GET" when HttpMethod is called
mockRequest.Stub(x => x.HttpMethod).Return("GET");
var controller = new AccountController();
// assign the fake context
var context = new ControllerContext(mockHttpContext,
new RouteData(),
controller);
controller.ControllerContext = context;
// act
...
- 3 回答
- 0 关注
- 613 浏览
添加回答
举报