How to clear previous expectations on an object?

There are three ways: You can reset the expectations by using BackToRecord I have to admit that I never really used it because it is awkward. // clear expectations, an enum defines which _stubRepository.BackToRecord(BackToRecordOptions.All); // go to replay again. _stubRepository.Replay(); Edit: Now I use it sometimes, it is actually the cleanest way. There should be … Read more

Mocking Static methods using Rhino.Mocks

Is it possible to mock a static method using Rhino.Mocks No, it is not possible. TypeMock can do this because it utilizes the CLR profiler to intercept and redirect calls. RhinoMocks, NMock, and Moq cannot do this because these libraries are simpler; they don’t use the CLR profiler APIs. They are simpler in that they … Read more

Mocking Asp.net-mvc Controller Context

Using MoQ it looks something like this: 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); I think the Rhino Mocks syntax is similar.

How to mock the Request on Controller in ASP.Net MVC?

Using Moq: var request = new Mock<HttpRequestBase>(); // Not working – IsAjaxRequest() is static extension method and cannot be mocked // request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */); // use this request.SetupGet(x => x.Headers).Returns( new System.Net.WebHeaderCollection { {“X-Requested-With”, “XMLHttpRequest”} }); var context = new Mock<HttpContextBase>(); context.SetupGet(x => x.Request).Returns(request.Object); var controller = new YourController(); controller.ControllerContext = … Read more