Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Expectation order

0 votes
I Need some explanation on this
MockManager.Init(true);
         Mock mock = MockManager.Mock(typeof(MainClass),false);
         mock.ExpectSet("I");

            MainClass mainClass=new MainClass();
            mainClass.I=1;
            
            mock.Verify();

that passes ok as expected
but the following is expected to pass also but it does not
MockManager.Init(true);
         Mock mock = MockManager.Mock(typeof(MainClass),false);
         

            MainClass mainClass=new MainClass();
            mainClass.I=1;
            
         mock.ExpectSet("I");
            MainClass mainClass1=new MainClass();
            mainClass1.I=2;
            mock.Verify();
asked Feb 1, 2005 by tolisss (3,660 points)

3 Answers

0 votes
I Need some explanation on this
the following is expected to pass also but it does not
MockManager.Init(true);
         Mock mock = MockManager.Mock(typeof(MainClass),false);
         

            MainClass mainClass=new MainClass();
            mainClass.I=1;
            
         mock.ExpectSet("I");
            MainClass mainClass1=new MainClass();
            mainClass1.I=2;
            mock.Verify();


Hi, This code should fail by definition. When you mock a type, you are mocking the NEXT instance of the type. In your example, you are mocking 1 instance: mainClass, but not the next instance. So mainClass1 is not mocked (The expectations is on mock that is linked to mainClass). You can do one of the following
1. mock the type again
2. use MockManager.MockAll() this will mock all instances of the type.

I hope that this explains it.
answered Feb 1, 2005 by scott (29,080 points)
0 votes
MockAll did part of the job thnks
But i have the following situation.
A third party class first sets a property value to false and then to whatever value I assign to it

How can i confirm the 2nd value of the property?
answered Feb 1, 2005 by tolisss (3,660 points)
0 votes
A third party class first sets a property value to false and then to whatever value I assign to it

How can i confirm the 2nd value of the property?


Hi this can be done in this way
// expect set no argument check
mock.ExpectSet("Prop");
// except set with argument check
mock.ExpectSet("Prop").Args(yourValue);
answered Feb 1, 2005 by scott (29,080 points)
...