You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(I know this is really a kind of duplicate of #1018, but since I am not authorized to comment on the closed issue, I am communicating it here.)
We had a test that was written like below (here simplified). Essentially the same as #1018 (hence the namespace), but in this case the "extra" .Setup is there for a legitimate reason. Here written without the use of MockRepository.
Steps to Reproduce
This full console app (C#) illustrates the situation:
usingMoq;namespaceMoq1018;staticclassProgram{staticvoidMain(){// set up allvarmockOne=newMock<IOne>(MockBehavior.Strict);varmockTwo=newMock<ITwo>(MockBehavior.Strict);mockOne.Setup(x =>x.GetSomething()).Returns(mockTwo.Object).Verifiable();mockTwo.Setup(x =>x.DoSomething()).Verifiable();// make SUTvarobjectToTest=newObjectToTest(mockOne.Object);// call first method and verifyobjectToTest.FirstMethod();mockOne.Verify();// call second method and verifyobjectToTest.SecondMethod();mockTwo.Verify();}}classObjectToTest(IOneone){ITwo?_Member;publicvoidFirstMethod(){_Member=one.GetSomething();}publicvoidSecondMethod(){_Member?.DoSomething();}}publicinterfaceIOne{ITwoGetSomething();}publicinterfaceITwo{voidDoSomething();}
Expected Behavior
It feels the above test (just called Main here) should complete without failure.
Actual Behavior
When control reaches mockOne.Verify(), an exception is thrown because the setup on mockTwo has not been met (yet)!
Known Workarounds
If we change overload of .Returns and write .Returns(() => mockTwo.Object) instead of simply .Returns(mockTwo.Object), then it works as expected. Saw this workaround in MockRepository.Verify() also verifies mocks created outside of the repo #1018, but it feels unnatural that this change should lead to another outcome.
If we move the mockTwo.Setup statement several lines down, to after the mockOne.Verify call, then the code runs without issue. This may give clearer test code in some cases, but above, the author wanted to keep all setups together in a single "section" of the test method.
If we move the mockOne.Verify() down to after the call to objectToTest.SecondMethod(), then it works. But then we cannot really prove if it was FirstMethod() or SecondMethod() that did GetSomething.
Maybe there is an entirely different way to write the test that I had not thought of?
(I know this is really a kind of duplicate of #1018, but since I am not authorized to comment on the closed issue, I am communicating it here.)
We had a test that was written like below (here simplified). Essentially the same as #1018 (hence the namespace), but in this case the "extra"
.Setupis there for a legitimate reason. Here written without the use ofMockRepository.Steps to Reproduce
This full console app (C#) illustrates the situation:
Expected Behavior
It feels the above test (just called
Mainhere) should complete without failure.Actual Behavior
When control reaches
mockOne.Verify(), an exception is thrown because the setup onmockTwohas not been met (yet)!Known Workarounds
.Returnsand write.Returns(() => mockTwo.Object)instead of simply.Returns(mockTwo.Object), then it works as expected. Saw this workaround in MockRepository.Verify() also verifies mocks created outside of the repo #1018, but it feels unnatural that this change should lead to another outcome.mockTwo.Setupstatement several lines down, to after themockOne.Verifycall, then the code runs without issue. This may give clearer test code in some cases, but above, the author wanted to keep all setups together in a single "section" of the test method.mockOne.Verify()down to after the call toobjectToTest.SecondMethod(), then it works. But then we cannot really prove if it wasFirstMethod()orSecondMethod()that didGetSomething.Version Info
Moq 4.20.72