We all know that in Moq, we can mock the method using Setup
. How can I check if the method is being setup? I don't want to invoke the method and check its result, because that will count during Verify
as an actual call to the mocked method (unless you can tell me how to make that not count - that will also count as an answer).

- 8,011
- 30
- 39
-
Setup method before running code under the test, then you will always know that method was setup – Fabio Jun 09 '17 at 16:53
-
1That is obvious. I have specific scenario where I need to check if I already did the setup or not. – Tengiz Jun 09 '17 at 16:55
-
2I don't believe there is any way to see what Moq has performed setup on, but I'm curious what use case you would have for this need? Every ounce of me is saying 'you are over-engineering a unit test' to have this need because mocks are by nature meant to be implied as setup during a unit test. I'm sure it's a valid reason, but I am genuinely curious what that reason is – Robert Petz Jun 09 '17 at 16:55
-
1In a complex codebase, I have to create a helper method which helps setup a complex structure. The setup itself is expensive since I'm setting up several nested objects. If somebody wants to setup a child object, I want to ensure that parent is already setup (parent setup is a prerequisite to child's setup). But I don't want to force every child setup to also setup the parent (helper method should take care of that). – Tengiz Jun 09 '17 at 16:57
-
1For unit tests, better if you setup dependencies before every test. It seems that your specific scenario leads you into "hack" workarounds, which is good sign that something gone wrong with design of your test. Can show example on describe your specific scenario – Fabio Jun 09 '17 at 16:58
-
Do you have any condition in your helper method? – Fabio Jun 09 '17 at 17:02
-
I'm trying to have. Condition I want to have is: if parent object was not setup yet, set it up Then setup the child object, because now it's safe to setup the child. – Tengiz Jun 09 '17 at 17:06
-
I gotta agree with @Fabio. You are solving a problem by introducing a new problem. If you *have* to rely on the helper setup method, then you should trust that the helper method is setup correctly and verify that by running your test. But you really shouldn't spread your setups across multiple locations in code conditionally at runtime because that will very much cause you headaches down the line. At least with the helper method you can clearly see what changed if your existing tests suddenly break. – Robert Petz Jun 09 '17 at 17:15
-
1I agree with the complexity complaints. I have such complaints myself, but sometimes we have to deal with what we inherit, including legacy or unwanted design. I'm just extending what was already there. – Tengiz Jun 09 '17 at 17:17
2 Answers
Actually, I just found ResetCalls
method defined on Mock
, which will reset the counter. That may help me, since I can call the parent method and compare the result with the expected result. If the method returns null, then I setup since it wasn't done yet, then I reset the counter by calling ResetCalls
(as if I never checked the result of the method). Then I continue with the child setup.
I need to watch for its side effects, but posting here in case somebody else has the same question. I still hope to find a better answer, such as maybe to reset the last call only to the mocked method. My current finding resets all the calls, which may not be what I want in all situations.

- 8,011
- 30
- 39
-
_If the method returns null, then I setup since it wasn't done yet_ - don't forget to write unit tests for your tests :) – Fabio Jun 09 '17 at 18:22
-
Right, we do have unit tests for this it turns out... but I'm totally with you. – Tengiz Jun 09 '17 at 20:39
There is a very dirty method with reflection, but it works.
using System.Linq;
using System.Linq.Expressions;
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
public static class MoqExtensions
{
public static bool WasMethodSetup(this Mock mock, string method)
{
var moqSetups = new PrivateObject(new PrivateObject(mock).GetProperty("Setups")).Invoke("ToArray") as object[];
var moqExpressions = moqSetups.Select(s => new PrivateObject(s).GetProperty("SetupExpression") as LambdaExpression);
return moqExpressions.Where(e => e.Body.NodeType == ExpressionType.Call)
.Select(b => new PrivateObject(new PrivateObject(b.Body).GetProperty("Method")).GetProperty("Name"))
.Contains(method);
}
}
Example of usage
var mock = new Mock<IDisposable>();
mock.Setup(d => d.Dispose());
var b1 = mock.WasMethodSetup("Dispose"); // true
var b2 = mock.WasMethodSetup("666"); // false
There is a problem when an unit test class do subclass another one, and tests of the subclass are already setup with subclass mocks. So there is only way to solve this problem is to setup subclass mocks to callback YOUR mock methods. Its.Ok(), but sometimes in your callbacks workflow it should be redirected to original mock method callback. Just searching how to invoke it from this mock list

- 301
- 2
- 13