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

Settings variable values in a Moq Callback() call

This is because the code in the Returns method is evaluated immediately; that is, when the Setup method is being invoked. However, the callback isn’t being invoked until the GetByFilter method is invoked. Luckily, the Returns method is overloaded so that you can defer its execution as well: mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())) .Callback((IFilter filter) => myFilteredFoos … 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

How to set up a method twice for different parameters with Moq

Moq supports this out of box with argument constraints: mock.Setup(ms => ms.ValidateUser( It.Is<string>(u => u == username), It.Is<string>(p => p == password)) .Returns(new ValidUserContext { Principal = principal }); mock.Setup(ms => ms.ValidateUser( It.Is<string>(u => u != username), It.Is<string>(p => p != password)) .Returns(new ValidUserContext()); Catch-all It.IsAny also works, but the order is important: // general … Read more

Moq ReturnsAsync() with parameters

From Moq v4.5.28 onwards You can use ReturnsAsync with lambdas, exactly as in the code example of the question. No need to use Task.FromResult() any more. You just need to specify the types of the lambda delegate arguments. Otherwise you will get the same error message: Cannot convert lambda expression to type ‘Model.WhitelistItem’ because it … Read more

Mock an update method returning a void with Moq

If you just want to verify this method is called, you should use Verifiable() method. _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>())) .Verifiable(); If you also want to do something with those parameters, use Callback() first. _mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>())) .Callback((int id, string lastName) => { //do something }).Verifiable(); Update Here’s how you should mock it if you … Read more

Reset mock verification in Moq?

This is now supported in Moq Use .Invocations.Clear() on the latest version of the library: var foo = new Mock<foo>(); foo.Invocations.Clear(); Old answer I think long after this post was created they added the functionality that the OP had asked for, there is a Moq extension method called Moq.MockExtensions.ResetCalls(). With this method you can do … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)