Schroedingers Cat
Schroedingers Cat

Reputation: 3139

Moq verify is not working as it looks like it should be

I have the following code in a test in my code. Note that this test was not written by me, the code it is calling was also not written by me, but I have changed it.

        mockedIMessage.Setup(m => m.ScriptMethodInvoker(EnumFunction.MsgFullSetPosition, It.IsAny<Result>())).Returns(1).Verifiable();
        var result = GetTestExecutionResult(script);
        mockedIMessage.Verify(m => m.ScriptMethodInvoker(EnumFunction.MsgFullSetPosition, It.IsAny<Result>()), Times.Once);

On the original code, this works. On mine, it doesn't - it tells me that the expected invocation on the mock was 0 times, not the once expected.

However, when I step it through in debug mode, it most definitely does call this code, and completes without throwing an exception. So the code appears to work, but the Moq is not detecting that it has been called.

Upvotes: 1

Views: 2880

Answers (1)

Good Night Nerd Pride
Good Night Nerd Pride

Reputation: 8500

From what you provided, multiple mistakes could have been made:

  • Make sure you are calling ScriptMethodInvoker() on the mocked IMessage instance
  • If ScriptMethodInvoker() has overloads, make sure the right one is called
  • Make sure EnumFunction is an enum
  • If EnumFunction is not an enum then make sure that EnumFunction.MsgFullSetPosition always returns the same instance
  • Make sure GetTestExecutionResult() actually calls ScriptMethodInvoker().

Upvotes: 1

Related Questions