Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

how to use Parasoft Dynamic stubs #1

wsubers
wsubers Posts: 7

Hello Community,

I am a first time user of Parasoft's dynamic stubs. I am evaluating this feature as an alternate to using Google mock.

If I have a function:

void Func1()
{
int a = 1;
Func2(a);
a++;
Func2(a);
}

In my test case for testing Func1, what is the appropriate Dynamic Stubs calls to validate the first time Func2 was called, the input parameter a was equal to 1 and the second time Func2 was called, the input parameter a was equal to 2?

Tagged:

Comments

  • Mirek
    Mirek Posts: 141 admin

    Hello,

    The simplest solution is to add the following two lines to the test case:

    CPPTEST_ON_CALL("Func2")->If()->LP()->RunId()->Equal()->Value(1)->RP()->If()->LP()->Arg("a")->NotEqual()->Value(1)->RP()->Fail("Argument \"a\" not equal 1");
    
    CPPTEST_ON_CALL("Func2")->If()->LP()->RunId()->Equal()->Value(2)->RP()->If()->LP()->Arg("a")->NotEqual()->Value(2)->RP()->Fail("Argument \"a\" not equal 2");
    

    I will discuss only the first one since the second one is exactly the same, there are 3 parts:

    1.
    CPPTEST_ON_CALL("Func2")->
    Register operation for the "Func2" stub id

    1. If()->LP()->RunId()->Equal()->Value(1)->RP()->
      Check the stub call number. Counting starts from 1.
      Remaining part will be executed only if this evaluates to true.

    3.
    If()->LP()->Arg("a")->NotEqual()->Value(1)->RP()->
    Check the value of parameter (this action is executed only if "RunId" condition evaluated to true)

    4.
    Fail("Argument \"a\"~~~~ not equal 1");
    Report failure (executed only if parameter check evaluated to true)

    Hope this helps