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
Hi there. I have created a dummy class and am testing both in natural and reflective mocks.

How come I need to define an expected call in the reflective mock example when I don't need to to define it in the natural mock?

EDIT: I am refering to why do i need to add the extra appleStackMock.ExpectCall("Push", 10); call?

Class
public static class AppleTree
    {
        private static Stack<Apple> apples;

        public static void NewSeason()
        {
            apples = new Stack<Apple>();

            for (int i = 0; i < 10; i++)
            {
                apples.Push(new Apple());
            }
        }

        public static Apple PickApple()
        {
            if(apples.Count > 0)
            {
                return apples.Pop();
            }
            else
            {
                throw new AppleException("There are no more apples to pick!");
            }
        }
}


Natural Mock
        [Test, VerifyMocks]
        [ExpectedException(typeof(AppleException))]
        public void TestExceptionIsThrownWhenThereAreNoMoreApplesToPick()
        {
            using (RecordExpectations rec = RecorderManager.StartRecording())
            {
                Stack<Apple> apples2 = new Stack<Apple>();
                rec.ExpectAndReturn(apples2.Count, 0);
            }

            AppleTree.NewSeason();
            AppleTree.PickApple();
        }


Reflective Mock
        [Test, VerifyMocks]
        [ExpectedException(typeof(AppleException))]
        public void ReflectiveMocks_TestExceptionIsThrownWhenThereAreNoMoreApplesToPick()
        {
            MockManager.Init();
            Mock appleStackMock = MockManager.Mock(typeof(Stack<Apple>));

            appleStackMock.ExpectCall("Push", 10);
            appleStackMock.ExpectGet("Count", 0);

            AppleTree.NewSeason();
            AppleTree.PickApple();
        }


Thanks!
asked by sebastianpatten (2.2k points)

2 Answers

0 votes
Hi
The difference is because in reflective mocks constructors are not mocked
by default.
You are getting an exception from the Push method because it references an un-initialized field inside the Stack class.
In natural mocks it doesn't happens because fields are mocked by default.

So your code should be:
public void ReflectiveMocks_TestExceptionIsThrownWhenThereAreNoMoreApplesToPick()
{
    Mock appleStackMock = MockManager.Mock(typeof(Stack<Apple>), Constructor.NotMocked);

    appleStackMock.ExpectGet("Count", 0);

    AppleTree.NewSeason();
    AppleTree.PickApple();
}


Hope it helps
:)
answered by ohad (35.4k points)
0 votes
Hi
Reading back my reply I noticed few mistakes, so here is the correct answer:
1. Reflective mocks constructors are mocked by default. (I had a typo in my previous answer)

2. I think that the main problem is in the concept of your code.
What you are trying to do is partial mock of the stack class. Usually it's not a good idea. A better practice is to mock all the methods of the stack class:

[Test]
[ExpectedException(typeof(AppleException))]
public void ReflectiveMocks_TestExceptionIsThrownWhenThereAreNoMoreApplesToPick()
{            
    Mock appleStackMock = MockManager.Mock(typeof(Stack<Apple>));

    appleStackMock.ExpectCall("Push", 10);
    appleStackMock.ExpectAndReturn("Pop", new Apple());
    appleStackMock.ExpectGet("Count", 0);

    AppleTree.NewSeason();
    AppleTree.PickApple();
    MockManager.Verify();
} 


:arrow: I used MockManager.Verify(); instead of VerifyMocks attribute since we have a bug when using VerifyMocks together with ExpectedException attribute.
answered by ohad (35.4k points)
...