Description
Failure description
I updated Moq from 4.13.1 to 4.15.2. This causes some of our tests to fail with the following exception:
System.InvalidOperationException : This call to 'Verifiable' will have no effect because conditional setups are ignored by both 'Verify' and 'VerifyAll'. This might indicate an error in your code.
This exception seems to be added with 4.14.0 (or to be more precise #997 ). To me it seems to be an error, because the code worked fine before the update here an example of a sequence test (with NUnit):
[Test]
public void TestMockSequence()
{
var myMock = new Mock<MyClass>();
var myTestObject = new MyTestObject(myMock.Object);
var sequence = new MockSequence {Cyclic = false};
myMock.InSequence(sequence).Setup(x => x.FirstCall()).Verifiable();
myMock.InSequence(sequence).Setup(x => x.SecondCall()).Verifiable();
myTestObject.TestMe();
myMock.Verify();
myMock.VerifyNoOtherCalls();
}
public class MyTestObject
{
private readonly MyClass _myMockObject;
public MyTestObject(MyClass myMockObject)
{
_myMockObject = myMockObject;
}
public void TestMe()
{
_myMockObject.FirstCall();
_myMockObject.SecondCall();
}
}
public class MyClass
{
public virtual void FirstCall()
{
}
public virtual void SecondCall()
{
}
}
With 4.13.1 it works as expected, if the order is changed the test fails otherwise it passes.
The line which does the validation is:
myMock.VerifyNoOtherCalls();
Which might be missleading, because on the first glace it looks like it is the verify line. But when I have a look at the Quickstart on MockSequence it seems to be the way this works because there it is added to a "Strict"-Mock.
Failed workaround
If I remove the .Verifiable()
like suggested in the error message and the update notes. VerifyNoOtherCalls
failes because it does not consider the sequence set up as a "real" set up.
Workaround
Changing the mock to a strict-mock and removing the verifiable from the calles does work.
This was not done in the test before because the mock is used for other test cases where no strict setup was requiered.
Expected behavoir
Either VerifyNoOtherCalls()
should not fail if a set up is done in a sequence w/o .Verifiable()
or .Verifiable()
should be allowed for sequence tests.
Activity
stakx commentedon Dec 8, 2020
Thanks for reporting. This appears to be a regression introduced by db0f208. I'll look into it soon.
stakx commentedon Dec 28, 2020
It appears we need to undo #997 in order to fix this regression.
.Verifiable()
once again #1121Merged PR 1366: Bump Moq from 4.15.2 to 4.16.0