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

I found that ExpectAndReturn may throw exception if used in conjunction with VerifyMock and having the expected method being called twice and written on the same line. Here's the sample code.

First, define two classes, namely SizeDT and epsilonT

   public class SizeDT
   {
      private double x;
      private double y;
      public SizeDT(double xx, double yy)
      {
         x = xx;
         y = yy;
      }
      public bool IsEmpty
      {
         get
         {
            bool xEp = epsilonT.IsEpsilon(this.x);
            bool yEp = epsilonT.IsEpsilon(this.y);
            if ( xEp&& yEp) 
             return true;
            return false;
         }
      }

      public bool IsEmpty2
      {
         get
         {

            if ( epsilonT.IsEpsilon(this.x)&& epsilonT.IsEpsilon(this.y))
               return true;
            return false;
         }
      }
   }

And
   public class epsilonT
   {
      public epsilonT()
      {

      }

      public static bool IsEpsilon(double ptValue)
      {
         return ptValue==0;
      }
   }


Now, run the following test class
using System;
using NUnit.Framework;
using TypeMock;
namespace ClassLibrary1
{

   [TestFixture]
   [ClearMocks]
   public class Class1
   {
      public Class1()
      {
         //
         // TODO: Add constructor logic here
         //
      }

      [SetUp]
      public void Init()
      {

      }

      [TearDown]
      public void TearDown()
      {

      }


      [Test]
      [VerifyMocks]
      public void CallDiffLines()
      {
         Mock mock = MockManager.Mock(typeof(epsilonT));
         mock.ExpectAndReturn("IsEpsilon", false);
         mock.ExpectAndReturn("IsEpsilon", false);
         SizeDT size = new SizeDT(0,0);
         Assert.IsFalse(size.IsEmpty);
      }

      [Test]
      [VerifyMocks]
      public void CallSameLines()
      {
         Mock mock = MockManager.Mock(typeof(epsilonT));
         mock.ExpectAndReturn("IsEpsilon", false);
         mock.ExpectAndReturn("IsEpsilon", false);
         SizeDT size = new SizeDT(0,0);
         Assert.IsFalse(size.IsEmpty2);
      }
   }
}


The CallSameLines method will return the following exception, but the CallDiffLines won't:

------ Test started: Assembly: ClassLibrary1.dll ------

TestCase 'ClassLibrary1.Class1.CallSameLines'
failed: TypeMock.VerifyException :
TypeMock Verification: Method ClassLibrary1.epsilonT.IsEpsilon() 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 settingssoon hui.esteemsoftmy documentsjobs hird party components ypemockclasslibrary1class1.cs(78,0): at ClassLibrary1.Class1.CallSameLines()


________
Lincoln-Zephyr V12 engine history
asked by nsoonhui (59.1k points)

2 Answers

0 votes
Hi,

I think what happens is as follows:
You set the 1st expectation to return false, and it works.

The "if" clause inside IsEmpty2 receives the return value "false" and doesn't go on to test the second clause - this is a compiler optimization, that if at least one clause doesn't satisfy the condition, there's no point testing the rest.

So the second mocked method does not get called, and you get the verification exception.

I think you should remove the second expectation - this will make the current code to work correctly.

      [Test] 
      [VerifyMocks] 
      public void CallSameLines() 
      { 
         Mock mock = MockManager.Mock(typeof(epsilonT)); 
         mock.ExpectAndReturn("IsEpsilon", false); 
         SizeDT size = new SizeDT(0,0); 
         Assert.IsFalse(size.IsEmpty2); 
      } 


Let me know if you need any more help.
answered by gilz (14.5k points)
0 votes
Hi,

I think what happens is as follows:
You set the 1st expectation to return false, and it works.

The "if" clause inside IsEmpty2 receives the return value "false" and doesn't go on to test the second clause - this is a compiler optimization, that if at least one clause doesn't satisfy the condition, there's no point testing the rest.

So the second mocked method does not get called, and you get the verification exception.

I think you should remove the second expectation - this will make the current code to work correctly.

      [Test] 
      [VerifyMocks] 
      public void CallSameLines() 
      { 
         Mock mock = MockManager.Mock(typeof(epsilonT)); 
         mock.ExpectAndReturn("IsEpsilon", false); 
         SizeDT size = new SizeDT(0,0); 
         Assert.IsFalse(size.IsEmpty2); 
      } 


Let me know if you need any more help.

Oh, thanks for your explanation. Now I got it. :o
________
MU-series
answered by nsoonhui (59.1k points)
...