Using Moq to verify calls are made in the correct order

There is bug when using MockSequence on same mock. It definitely will be fixed in later releases of Moq library (you can also fix it manually by changing Moq.MethodCall.Matches implementation). If you want to use Moq only, then you can verify method call order via callbacks: int callOrder = 0; writerMock.Setup(x => x.Write(expectedType)).Callback(() => Assert.That(callOrder++, … Read more

Mocking generic methods in Moq without specifying T

In Moq 4.13 the It.IsAnyType and It.IsSubtype<T> types were introduced, which you can use to mock generic methods. E.g.: public interface IFoo { bool M1<T>(); bool M2<T>(T arg); } var mock = new Mock<IFoo>(); // matches any type argument: mock.Setup(m => m.M1<It.IsAnyType>()).Returns(true); // matches only type arguments that are subtypes of / implement T: mock.Setup(m … Read more

How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?

I believe your only choice right now is to explicitly include the bool parameter in the setup for Foo. I don’t think it defeats the purpose of specifying a default value. The default value is a convenience for calling code, but I think that you should be explicit in your tests. Say you could leave … Read more

How to unit test with ILogger in ASP.NET Core

Just mock it as well as any other dependency: var mock = new Mock<ILogger<BlogController>>(); ILogger<BlogController> logger = mock.Object; var controller = new BlogController(logger); or use this short equivalent: logger = Mock.Of<ILogger<BlogController>>() You probably will need to install Microsoft.Extensions.Logging.Abstractions package to use ILogger<T>. Moreover, you can create a real logger: var serviceProvider = new ServiceCollection() .AddLogging() … Read more

What is the difference between passing It.IsAny() and the value of It.IsAny() to a method setup

It.IsAny only allows Moq to match future invocations of method calls if used within the Setup construct. When Setup is called Moq just adds the method call to its cache of already-set-up method calls. Note that the argument to Setup in your example has type Expression<Func<IFoo, bool>>. Since you are passing in an Expression, the … Read more

How do you mock the session object collection using Moq

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

Moq + Unit Testing – System.Reflection.TargetParameterCountException: Parameter count mismatch

It’s your Returns clause. You have a 4 parameter method that you’re setting up, but you’re only using a 1 parameter lambda. I ran the following without issue: [TestMethod] public void IValueConverter() { var myStub = new Mock<IValueConverter>(); myStub.Setup(conv => conv.Convert(It.IsAny<object>(), It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<CultureInfo>())). Returns((object one, Type two, object three, CultureInfo four) => (int)one + … Read more

Using Moq to mock only some methods

This is called a partial mock, and the way I know to do it in Moq requires mocking the class rather than the interface and then setting the “Callbase” property on your mocked object to “true”. This will require making all the methods and properties of the class you are testing virtual. Assuming this isn’t … Read more

How to mock ConfigurationManager.AppSettings with moq

I am using AspnetMvc4. A moment ago I wrote ConfigurationManager.AppSettings[“mykey”] = “myvalue”; in my test method and it worked perfectly. Explanation: the test method runs in a context with app settings taken from, typically a web.config or myapp.config. ConfigurationsManager can reach this application-global object and manipulate it. Though: If you have a test runner running … Read more

tech