Skip to content

System.InvalidOperationException because of call to Verifiable in MockSequence after 4.14.0 #1114

Closed
@Lyra2108

Description

@Lyra2108

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

stakx commented on Dec 8, 2020

@stakx
Contributor

Thanks for reporting. This appears to be a regression introduced by db0f208. I'll look into it soon.

self-assigned this
on Dec 28, 2020
added this to the 4.15.3 milestone on Dec 28, 2020
stakx

stakx commented on Dec 28, 2020

@stakx
Contributor

It appears we need to undo #997 in order to fix this regression.

modified the milestones: 4.15.3, 4.16.0 on Jan 1, 2021
locked and limited conversation to collaborators on Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @stakx@Lyra2108

    Issue actions

      System.InvalidOperationException because of call to Verifiable in MockSequence after 4.14.0 · Issue #1114 · devlooped/moq