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
In Isolator++ is it possible to confirm that a function was called with certain exact arguments? In C# Isolator this is possible with Isolate.Verify.WasCalledWithExactArguments().

This test below unexpectedly passes:

class TestFixture : public ::testing::Test
{
public:
   virtual void TearDown()
   {
      ISOLATOR_CLEANUP();
   }
};

class A
{
public:
   void f(int i)
   {
   }
};

TEST_F(TestFixture, Test)
{
   A* fakeA = FAKE<A>();

   fakeA->f(1);

   ASSERT_WAS_CALLED(fakeA->f(2)); // Passes despite f(1) and not f(2) having been called
}
asked by NeilJustice (14.1k points)

5 Answers

0 votes
Hi Neil,

In Isolator++ you can simulate the "Isolate.Verify.WasCalledWithExactArguments()" Isolator API
with a the DoMemberFunctionInstead function.
Use WhenCalled().DoMemberFunctionInstead([pointer to the alternative object],[alternative function])

The idea is to create your own alternative function (within custom class) whose logic is executed only for the appropriate parameters. This can be implemented either by Asser_EQ function or a simple if statement.

You are welcome to read further information in the following link:
http://docs.typemock.com/Isolatorpp/Con ... redica.htm
answered by Shai Barak (1.5k points)
0 votes
Hi Shai,

Thanks for the tip to use DoMemberFunctionInstead().

Isolate.Verify.WasCalledWithExactArguments() in Isolator++:

class TestFixture : public ::testing::Test
{
public:
   virtual void TearDown()
   {
      ISOLATOR_CLEANUP();
   }
};

class A
{
public:
   void f(int i)
   {
      throw "Unexpectedly called";
   }
};

class TESTABLE A_AssertWasCalled
{
public:
   void Assert_f_CalledWith(int iArg)
   {
      ASSERT_EQ(1, iArg);
   }
};

TEST_F(TestFixture, Test)
{
   A* fakeA = FAKE<A>();
   WHEN_CALLED(fakeA->f(_)).DoMemberFunctionInstead(&A_AssertWasCalled(), Assert_f_CalledWith);

   fakeA->f(1);

   ASSERT_WAS_CALLED(fakeA->f(_));
}

While figuring out the above code I encounted this interesting error message in which the meaning of "testable" is unclear:

[ img ]
answered by NeilJustice (14.1k points)
0 votes
Hi Neil,

The TESTABLE is needed when you put your code-under-test within the test. Specifically, Isolator is designed to work when the tested code is in an external DLL. When putting the code-under-test in the test executable, Isolator has a problem working with it, and the TESTABLE def (which is basically a declspec(dllexport)) makes sure Isolator can work with it.

Regardless, in your tests you have to declare a real obj of class A in order to run f(1). FAKE makes sure that f() gets ignored, by default.

Also you have to declare A_AssertWasCalled obj which you send to WHEN_CALLED function, like this :

TEST_F(TestFixture, Test)
{
   A* realA = new A();
   
   A_AssertWasCalled* alt = new A_AssertWasCalled();
   WHEN_CALLED(realA->f(_)).DoMemberFunctionInstead(alt, Assert_f_CalledWith);
   
   realA->f(1);

   ASSERT_WAS_CALLED(realA->f(_));
}


Let me know if this helps.
answered by Shai Barak (1.5k points)
0 votes
Hi Neil,

Have you found my suggestion helpful ?
I hope you managed to solve your issue.

Let me know if you need any help.
answered by Shai Barak (1.5k points)
0 votes
Hi Shai,

Yes, I found your description of TESTABLE helpful. Thanks!
answered by NeilJustice (14.1k points)
...