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
I expect the two first ExpectAndReturns to cause a test failure but the don't, and I can't figure out why.

        [Test, VerifyMocks]
        public void Q_TestTrueObject()
        {
            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                recorder.VerifyMode = VerifyMode.Normal; //Normal is default, just checking
                ToMock tomock = new ToMock();
                recorder.CheckArguments();

                BoolObject boolobject = new BoolObject();
                recorder.ExpectAndReturn("_thisdoesnotexist_CallGetTrueObject", boolobject);            //OK but should fail
                //recorder.ExpectAndReturn("tomock._thisdoesnotexist_CallGetTrueObject", boolobject);   //OK but should fail
                //recorder.ExpectAndReturn("tomock.ReturnTrueObject", boolobject);                      //OK, obj exists
                recorder.CheckArguments();
            }
            ToTest totest = new ToTest();
            BoolObject t = totest.CallReturnTrueObject();

            Assert.IsFalse(t.GetBool);//The mocked object is NOT constructed and therefore false ! ! !
            Assert.AreEqual(0, ToMock.constructioncount); //Simple check to see if the constructor is called at all - set to 1 to crash

            MockManager.Verify();
        }


I haven't included the ToMock and ToTest objects, as these are trivial...

Any clues will be appreciated
/Jes
asked by Larovia (3.8k points)

3 Answers

0 votes
Hi,

Inside a recording block we don't use string to set expectations we use real statements.
For example instead of:
recorder.ExpectAndReturn("tomock.ReturnTrueObject", boolobject);

you should write:
recorder.ExpectAndReturn(tomock.ReturnTrueObject(), boolobject);

you can also write the same this way (which I find easier to understand)
tomock.ReturnTrueObject();
recorder.Return(boolobject);


This is the true strength if Natural mocking.

Also please note that using object created inside the recording block (boolobject) which are mocked as return values is not recommended. The system might lose all reference to it and it may be disposed.

:idea: You can use recorder.DefaultBehavior.CheckArguments(); in order to make sure that argument will be checked on all calls.
answered by lior (13.2k points)
0 votes
The "tomock.ReturnTrueObject" seems to work as expected, but the others did not cause a failure such as "object / method not found". It was only for creating tests for objects that does not exist yet, and therefore can't use code completion. It does not matter though.

recorder.Return says "The mocked return value" in the API, what is the recommended way to mock return objects as mine ? Create them before using{ . . } or ?

Regards
/Jes
answered by Larovia (3.8k points)
0 votes
Hi,

I probably dint explain this well enough.
Passing Strings as the first argument to recorder.ExpectAndReturn has no meaning. (string are used only by the MockManager class)

Even if you are creating tests for none existing classes you should use the real methods (as you expect them to be) this of course means that the test code wont compile until the real code is in place, but this is OK (part of the TDD approach).

Regarding creation of fake return value it really depends on what you want.
if you just need to return a specific value easiest way is to create it outside the using block.
if you need to return an object that is also mocked (i.e. you want to set expectation on it as well) use the CreateMockedObject API.
answered by lior (13.2k points)
...