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

I found that The ExpectSet method does not seem to work properly with the following test code:
   public class epsilonT
   {
      private double tol;
      public double Tol
      {
         get { return tol; }
         set { tol = value; }
      }
      public epsilonT()
      {

      }
      }

   public class SizeDT
   {
      private double x;
      public double X
      {
         get { return x; }
         set { x = value; }
      }
      private double y;
      public double Y
      {
         get { return y; }
         set { y = value; }
      }
      public SizeDT(double xx, double yy)
      {
         x = xx;
         y = yy;
      }

      public static SizeDT Empty
      {
         get
         {
            return new SizeDT(0,0);
         }
      }


      public void TestEps()
      {
         epsilonT eps = new epsilonT();
         eps.Tol=10;
      }
      public void ArgumentTest()
      {
         x = 10;
         y =10;
         SizeDT size = new SizeDT(x,y);
         size.GreatStuff(100);
         X = size.x;
         X = -40;
         Y = size.y;
      }

   }


   [TestFixture]
   [ClearMocks]
   public class Class1
   {
      [Test]
      [VerifyMocks]
      public void TestExpectSet2()
      {
         SizeDT size = SizeDT.Empty;
         Mock mock = MockManager.Mock(typeof(SizeDT));
         mock.ExpectSet("X");
         size.ArgumentTest();
      }
          }

I would get the below exception
TestCase 'ClassLibrary1.Class1.TestExpectSet2'
failed: TypeMock.VerifyException :
TypeMock Verification: Method ClassLibrary1.SizeDT.set_X() has 1 more expected calls
at TypeMock.MockManager.Verify()
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.d()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
c:\documents and settings\soon hui.esteemsoft\my documents\jobs\third party components\typemock\classlibrary1\testclass.cs(126,0): at ClassLibrary1.Class1.TestExpectSet2()

________
how to roll a blunt
asked by nsoonhui (59.1k points)

7 Answers

0 votes
Hi,

Here's your test:
      [Test] 
      [VerifyMocks] 
      public void TestExpectSet2() 
      { 
         SizeDT size = SizeDT.Empty; 
         Mock mock = MockManager.Mock(typeof(SizeDT)); 
         mock.ExpectSet("X"); 
         size.ArgumentTest(); 
      } 


Notice that you call MockManager.Mock after you create "size" the instance you later call the function on.

Here's what happens: Mock means the next time you see a "new" it will be mocked. The next time a "new" is seen is inside ArgumentTest:

      public void ArgumentTest() 
      { 
         x = 10; 
         y =10; 
         SizeDT size = new SizeDT(x,y); 
         size.GreatStuff(100); 
         X = size.x; 
         X = -40; 
         Y = size.y; 
      } 


Note that this is a local "size". So this one is mocked, but the external "size" is not. Now, in the lines:

         X = size.x; 
         X = -40; 
         Y = size.y; 


The GET properties of the local (and mocked) "size" are called, and they are set into the external (and not mocked) "size". . However, since we didn't satisfy the expectation we set on the external "size", but on the local "size", and we didn't set anything on the local size.x, you get the verification exception.

So what you need to do is call Set on the instance you plan to mock.

Let me know if I can help further.
answered by gilz (14.5k points)
0 votes
Hi,

Here's your test:
      [Test] 
      [VerifyMocks] 
      public void TestExpectSet2() 
      { 
         SizeDT size = SizeDT.Empty; 
         Mock mock = MockManager.Mock(typeof(SizeDT)); 
         mock.ExpectSet("X"); 
         size.ArgumentTest(); 
      } 


Notice that you call MockManager.Mock after you create "size" the instance you later call the function on.

Here's what happens: Mock means the next time you see a "new" it will be mocked. The next time a "new" is seen is inside ArgumentTest:

      public void ArgumentTest() 
      { 
         x = 10; 
         y =10; 
         SizeDT size = new SizeDT(x,y); 
         size.GreatStuff(100); 
         X = size.x; 
         X = -40; 
         Y = size.y; 
      } 


Note that this is a local "size". So this one is mocked, but the external "size" is not. Now, in the lines:

         X = size.x; 
         X = -40; 
         Y = size.y; 


The GET properties of the local (and mocked) "size" are called, and they are set into the external (and not mocked) "size". . However, since we didn't satisfy the expectation we set on the external "size", but on the local "size", and we didn't set anything on the local size.x, you get the verification exception.

So what you need to do is call Set on the instance you plan to mock.

Let me know if I can help further.


I see, thanks for your explanation..
________
Toyota AA
answered by nsoonhui (59.1k points)
0 votes
Hi,
I am curious about how does the
ExpectSet("X", noOfRuns)
work.

Continuing on the previous example, I modified my test to the following
      [Test]
      [VerifyMocks]
      public void TestExpectSet2()
      {
                        int noOfRuns = 1;
         Mock mock = MockManager.Mock(typeof(SizeDT));
         mock.ExpectSet("X", noOfRuns);
         SizeDT size = SizeDT.Empty;
         size.ArgumentTest();


and the test passes. Good.

So I change the noOfRuns from 1 to 2, and it still passes.

If I change the noOfRuns to 3, then the test fails because
TestCase 'ClassLibrary1.Class1.TestExpectSet2'
failed: TypeMock.VerifyException :
TypeMock Verification: Method ClassLibrary1.SizeDT.set_X() has 1 more expected calls
at TypeMock.MockManager.Verify()
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.d()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
c:\documents and settings\soon hui.esteemsoft\my documents\jobs\third party components\typemock\classlibrary1\testclass.cs(126,0): at ClassLibrary1.Class1.TestExpectSet2()


As far as I know, the noOfRun specifies how many times you expect a property to be set. If you expect it to be set 1 times, then you can't expect it to be set 2 times, and the converse is true, isn't it? How can you have a property to be set 1 and 2 times at the same time?
________
vaporizer review
answered by nsoonhui (59.1k points)
0 votes
Oops, I forgot to write down the modified ArgumentTest() function. This is the modified ArgumentTest() function:
      public void ArgumentTest()
      {
         x = 10;
         y =10;
         SizeDT size = new SizeDT(x,y);
         size.GreatStuff(100);
         X = size.x;
         X = -40;
         Y = size.y;
      }

________
Ford FN10 platform specifications
answered by nsoonhui (59.1k points)
0 votes
Hi,
I am curious about how does the
ExpectSet("X", noOfRuns)
work.

Continuing on the previous example, I modified my test to the following
      [Test]
      [VerifyMocks]
      public void TestExpectSet2()
      {
                        int noOfRuns = 1;
         Mock mock = MockManager.Mock(typeof(SizeDT));
         mock.ExpectSet("X", noOfRuns);
         SizeDT size = SizeDT.Empty;
         size.ArgumentTest();


and the test passes. Good.

So I change the noOfRuns from 1 to 2, and it still passes.

If I change the noOfRuns to 3, then the test fails because
TestCase 'ClassLibrary1.Class1.TestExpectSet2'
failed: TypeMock.VerifyException :
TypeMock Verification: Method ClassLibrary1.SizeDT.set_X() has 1 more expected calls
at TypeMock.MockManager.Verify()
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.d()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
c:\documents and settings\soon hui.esteemsoft\my documents\jobs\third party components\typemock\classlibrary1\testclass.cs(126,0): at ClassLibrary1.Class1.TestExpectSet2()


As far as I know, the noOfRun specifies how many times you expect a property to be set. If you expect it to be set 1 times, then you can't expect it to be set 2 times, and the converse is true, isn't it? How can you have a property to be set 1 and 2 times at the same time?


Sorry for the confusion, I think I get the issue resolved.
________
Mazda Navajo picture
answered by nsoonhui (59.1k points)
0 votes
Hi,

What is always helpful, is using the Trace tool - found under the Program Files->TypeMock.Net->Tracer. It can show you all expectations, on which instances, mocked and unexpected calls.
answered by gilz (14.5k points)
0 votes
Hi,

What is always helpful, is using the Trace tool - found under the Program Files->TypeMock.Net->Tracer. It can show you all expectations, on which instances, mocked and unexpected calls.


gilz, thanks for the information. :o
________
AJD-V6/PSA DT17
answered by nsoonhui (59.1k points)
...