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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
1st case:
            using (RecordExpectations recorder = new RecordExpectations())
            {
                Class1 c1 = Class2.GetClass1();
            }


After start returns the error:

------ Test started: Assembly: ClassLibrary1.dll ------
TestCase 'ClassLibrary1.Test.Foo'
failed: TypeMock.TypeMockException:
*** Cannot return a value for Class2.GetClass1() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly.
at TypeMock.RecorderManager.e()
at TypeMock.RecordExpectations.Dispose()
D:Old ProjectsQQQQQQClassLibrary1Class1.cs(59,0): at ClassLibrary1.Test.Foo()
0 passed, 1 failed, 0 skipped, took 8,13 seconds.


2nd case:
            using (RecordExpectations recorder = new RecordExpectations())
            {
                Class1 c1 = Class2.GetClass1();
                c1.Foo2();
            }


Works fine.
I feel that of both cases should fails or both should work.
Comments?
asked by tom3m (9.7k points)

3 Answers

0 votes
Hi,

In the 1st case, the engine identifies you haven't "closed" the call using recorder.Return. Obviously, then you get the exception.

What you see in the 2nd case is a feature of the mocking engine. It identifies the two sequential calls as chained calls. Here's why:

The 1st call returns a mocked instance of Class1. It then goes to record a call on that mocked object. This is similar to writing:
Class2.GetClass1().Foo2();


In this case, a Return call in the middle is unnecessary.
answered by gilz (14.5k points)
0 votes
Thanks, sounds reasonable, but why it works in that case ;)

            using (RecordExpectations recorder = new RecordExpectations())
            {
                Class1 c1 = Class2.GetClass1();
                c1.Foo();
                Class1 c2 = Class2.GetClass1();
                c2.Foo();
                c1.Foo2();
            };
[/code]
answered by tom3m (9.7k points)
0 votes
Hi,

The first and second calls are chained. So are the third and fourth. And so the former explanation applies to both.

The 5th call does not return a value (a void call), and therefore does not need a call to recorder.Return. Therefore, you don't get the exception.
If Foo2 returns something (e.g. bool), you will get an exception.
answered by gilz (14.5k points)
...