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
Hello,

I'm not quite sure how you set this up in TypeMock, using Natural or Reflective mocks. I have a class Menu, which has another property Parent. If parent is not null, then a method gets called on the parent object. If parent is null, then a value of false is returned from the method (nothing gets done essentially).

How do I ensure this is working correctly using TypeMock?

Thanks.
asked by bmains (13.2k points)

1 Answer

0 votes
Hi,

If I understand correctly, you have something like this:

    public class Menu
    {
        private Parent m_Parent;

        public Parent Parent
        {
            get { return m_Parent;}
            set { m_Parent = value;}
        }

        public bool Do()
        {
            if (Parent != null)
            {
                return Parent.Foo();
            }
            else
            {
                return false;
            }
        }
    }

    public class Parent
    {
        public bool Foo() 
        {
            return false;
        }
    }


And you want to mock both the call to the Parent property, and the call to Foo function. Here's a sample using reflective mocks:

        [TestMethod,VerifyMocks]
        public void TestWithMockingTheProperty()
        {
            MockObject<Parent> mockParent = MockManager.MockObject<Parent>();
            mockParent.ExpectAndReturn("Foo", true);
            Parent parent = mockParent.Object;

            Mock mockMenu = MockManager.Mock<Menu>();
            mockMenu.ExpectGetAlways("Parent", parent);
            
            Menu menu = new Menu();
            Assert.IsTrue(menu.Do());
        }


And in Naturals:
        [TestMethod,VerifyMocks]
        public void TestWithMockingThePropertyNatural()
        {
            Menu menu = new Menu();
            using (RecordExpectations rec = new RecordExpectations())
            {
                Parent fake = menu.Parent;
                rec.RepeatAlways();
                rec.ExpectAndReturn(fake.Foo(), true);
            }
            Assert.IsTrue(menu.Do());
        }


Here's how to check the null part:
        [TestMethod, VerifyMocks]
        public void TestWithMockingThePropertyNullNatural()
        {
            Menu menu = new Menu();
            using (RecordExpectations rec = new RecordExpectations())
            {
                rec.ExpectAndReturn(menu.Parent, null).RepeatAlways();
            }
            Assert.IsFalse(menu.Do());
        }


Let me know if this works for you.
answered by gilz (14.5k points)
...