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
I need mock several property get calls.

I have 3 Get. I want to get different value in sequence. For example, I want to get '1' at the first call, and '2' for the second call. How can I do that?

Thanks a lot!!!
asked by Ted2006 (600 points)

1 Answer

0 votes
Hi

This is very simple.
You can use ExpectGet method of the Mock class.
Example:

    public class ClassToMock
    {
        private int myVar;

        public int MyVar
        {
            get { return myVar; }
        }

        public ClassToMock()
        {
            myVar = 123;
        }
    }

    public class MyClass
    {
        public int a, b, c;

        public void Func()
        {
            ClassToMock cl = new ClassToMock();
            a = cl.MyVar;
            b = cl.MyVar;
            c = cl.MyVar;            
        }
    }

    public class TestClass
    {
        [Test]
        public void TstFunc()
        {
            MockManager.Init();
            Mock mock = MockManager.Mock(typeof(ClassToMock));
            mock.ExpectGet("MyVar", 1);
            mock.ExpectGet("MyVar", 2);
            mock.ExpectGet("MyVar", 3);
            MyClass myclass = new MyClass();
            myclass.Func();

            Assert.AreEqual(1, myclass.a);
            Assert.AreEqual(2, myclass.b);
            Assert.AreEqual(3, myclass.c);
            MockManager.Verify();
        }
    }


Hope it helps. :)
answered by ohad (35.4k points)
...