chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello,

I mock using natural mocks method calls on an internal class member variable. The code looks like this:

using (RecordExpectations recorder = new RecordExpectations())
{
recorder.VerifyMode = VerifyMode.PassIfNotCalled;
obj.member.IsReady();
recorder.Return(true).RepeatAlways();
}

The unit test has a call to IsReady method on obj.member. Everything works fine if I test from VS 2005. When I run tests from command-line (I do execute mocking_on.bat), then sometimes "recorder.Return" call throws the following exception:

*** Cannot use Return in this sequence, there must be a mocked statment first
at TypeMock.RecordExpectations.a(String A_0)
at TypeMock.RecordExpectations.Return(Object returnValue)

So it looks it ignores my call to obj.IsReady. But not always! What can be wrong? Is there any workaround?

Thanks for any tips

Vagif Abilov
asked by vagif (19.4k points)

5 Answers

0 votes
Well, I have to correct myself. "Occasional" error only happenned when I enabled code coverage _without_ selecting code coverage tool in TypeMock configurator. Once I selected NCover, all tests ran fine.
answered by vagif (19.4k points)
0 votes
Hi,
Thanks for pointing this out. A Better way would be to use the TMockRunner, this will allow you to change the Coverage per run.

:arrow: BTW there is no need to call VerifyMode.PassIfNotCalled when using RepeatAlways, as RepeatAlways doesn't fail if the call was never called.
To validate that a call was called at least once, you would have to use:
Repeat(1);
and then anther
RepeatAlways();

:idea: As we haven't recieved any requests for this, I guess that nobody really validates calls with an 'at least' logic.
answered by scott (32k points)
0 votes
Scott,

Thanks for the feedback. Yes, I could use TMockRunner, but I already had an MSBuild project file responsible for building our code, so I used "if it ain't broke don't fix it" approach :D

I understand now RepeatAlways won't fail if the call is not performed. Thanks for pointing this out.
answered by vagif (19.4k points)
0 votes
If you are using MSBuild, you should use the TypeMock MSBuild tasks.
This way you won't have to manually link NCover before running the tests.

Example:
<Target Name="TestWithTypeMock">
   <TypeMockStart Link ="NCover" ProfilerLaunchedFirst="true"/>
   <Exec Command=" $(NCover) $(NUnit) Test.dll"/>
   <TypeMockStop/>
</Target>

See TypeMock.NET MSBuild Tasks for more information
answered by scott (32k points)
0 votes
Oh that's very useful for us. Thanks Scott!
answered by vagif (19.4k points)
...