1

I start using gmock for my code recently, some of which rely on some other outside services or interfaces, so gmock is kind of perfect for me.

Say I have a trivial class A to mock

class A
{
    void foo(int& bar) {
        // will change bar
        bar = GetingResultFromSomeOutsideService();
    }
};

Now I create a mock class

class MockA: public A
{
    MOCK_METHOD1(foo, void(int& bar));
};

In my test case, I try to mock the behavior of A::foo of changing bar with ON_CALL, but I don't know how exactly.

For example, I want my MockA::foo always set bar to 1, so that my other codes invoking A::foo will always get bar == 1 like this:

// some other piece of codes invoking A::foo
int bar = 0;
A my_a;
my_a.foo(bar);
cout << bar;   // always output "1" with gmock

Any hint?

273K
  • 29,503
  • 10
  • 41
  • 64
Eric Zheng
  • 1,084
  • 1
  • 11
  • 23
  • 1
    Possible duplicate of [how to set custom ref-variable in gmock](https://stackoverflow.com/questions/8845753/how-to-set-custom-ref-variable-in-gmock) – Eric Zheng Aug 08 '17 at 07:25

0 Answers0