Mocking objects with Moq when constructor has parameters
Change the last line to var syncEngine = new Mock<CustomerSyncEngine>(mockLogger, mockCrm, mockCache).Object; and it should work
Change the last line to var syncEngine = new Mock<CustomerSyncEngine>(mockLogger, mockCrm, mockCache).Object; and it should work
Moq creates an implementation of the mocked type. If the type is an interface, it creates a class that implements the interface. If the type is a class, it creates an inherited class, and the members of that inherited class call the base class. But in order to do that it has to override the … Read more
Webforms is notoriously untestable for this exact reason – a lot of code can rely on static classes in the asp.net pipeline. In order to test this with Moq, you need to refactor your GetSecurityContextUserName() method to use dependency injection with an HttpContextBase object. HttpContextWrapper resides in System.Web.Abstractions, which ships with .Net 3.5. It is … Read more
I don’t think it is necessary to use a mock for the Func. You can simply create an ordinary Func yourself that returns a mock of IFooBarProxy: int numberOfCalls = 0; Func<IFooBarProxy> func = () => { ++numberOfCalls; return new Mock<IFooBarProxy>(); }; var sut = new FooBar(func); sut.Process(); Assert.Equal(2, numberOfCalls);
When setting up an IProtectedMock, you should use Moq.Protected.ItExpr instead of Moq.It. Here is the corrected implementation of what I was trying to do in my question: _innerHandler.Protected() .Setup<Task<HttpResponseMessage>>(“SendAsync”, ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .ReturnsAsync(responseMessage);
Thanks to those that responded over the last few weeks. I ended up writing a blog post about my findings, since I had to do so much digging that it seemed like I might as well summarize them in the hopes of helping others. The chart I posted organizes my findings: One caveat, which I … Read more
mockInvoice.VerifySet(x => x.InvoiceAttachmentId = 123, Times.Once()); Replace 123 with the expected value. If you want to permit any value, use: mockInvoice.VerifySet(x => x.InvoiceAttachmentId = It.IsAny<int>(), Times.Once());
Couldn’t you use AsQueryable()? List<Country> countries = new List<Country>(); // Add Countries… IQueryable<Country> queryableCountries = countries.AsQueryable(); geographicsRepository.Setup(x => x.GetCountries()).Returns(queryableCountries);
This problem occurs because you are trying to mock Select method, which is an extension method, not an instance method of IEnumerable<T>. Basically, there is no way to mock an extension method. Have a look at this question for some ideas that you may find useful. UPD (12/11/2014): To gain more understanding on mocking extension … Read more
I started with Scott Hanselman’s MVCMockHelper, added a small class and made the modifications shown below to allow the controller to use Session normally and the unit test to verify the values that were set by the controller. /// <summary> /// A Class to allow simulation of SessionObject /// </summary> public class MockHttpSession : HttpSessionStateBase … Read more