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 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 by tolisss (28.8k 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 by scott (32k 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 by tolisss (28.8k 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 by scott (32k points)
...