9
如何在ASP.Net MVC中模拟控制器上的请求?
我在使用ASP.Net MVC框架的C#中有一个控制器 public class HomeController:Controller{ public ActionResult Index() { if (Request.IsAjaxRequest()) { //do some ajaxy stuff } return View("Index"); } } 我有一些关于模拟的技巧,并希望通过以下代码和RhinoMocks来测试代码 var mocks = new MockRepository(); var mockedhttpContext = mocks.DynamicMock<HttpContextBase>(); var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>(); SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest); var controller = new HomeController(); controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller); var result …