Asp.net-Mvc

asp.net MVC中的單元測試,如何模擬頁面請求?

  • June 28, 2011

如何模擬 .net MVC 頁面的頁面請求?

使用 RhinoMocks:

var httpContext = MockRepository.GenerateMock<HttpContextBase>();
var httpRequest = MockRepository.GenerateMock<HttpRequestBase>();

httpContext.Expect( c => c.Request ).Return( httpRequest ).Repeat.Any();

... set up expectations on request...

var controller = new MyController();
controller.ControllerContext = new ControllerContext( httpContext,
                                                      new RouteData(),
                                                      controller );

...invoke action, check assertions...

httpContext.VerifyAllExpectations();
httpRequest.VerifyAllExpectations();

引用自:https://stackoverflow.com/questions/774204