How to Set Value of Complex Output Argument

4,532 views
Skip to first unread message

Kenneth

unread,
Oct 21, 2011, 3:59:02 AM10/21/11
to Google C++ Mocking Framework
Hi everyone,

Can someone please let me know how to set a complex output argument?
The cookbook only shows how to do so for simple types and arrays.
http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Side_Effects

I need to know how the mock function can set values in a class
instances.

Thanks,

Kenneth

Greg Miller

unread,
Oct 21, 2011, 9:39:11 AM10/21/11
to Kenneth, Google C++ Mocking Framework
Hi, Kenneth.

I'm not sure I completely understand what you're describing. It might help if you can provide the function signature that you'd like to mock, and a description of what you'd like to happen.

My guess, is that you'd like to have an action that given a pointer to a class, sets a particular field of that class. One way to do that would be with a simple custom action. For example:

struct Blah {
  int a;
  int b;
};

class Foo {
 public:
  virtual ~Foo() {}
  virtual void DoBlah(Blah* blah) = 0;
};

class MockFoo : public Foo {
 public:
  MOCK_METHOD1(DoBlah, void(Blah*));
};

class GmockTest : public ::testing::Test {
 protected:
};

// The simple custom action
ACTION(SetB) {
  Blah* the_blah = arg0;
  the_blah->b = 77;

TEST_F(GmockTest, DoesFoo) {
  MockFoo foo;
  EXPECT_CALL(foo, DoBlah(::testing::_))
      .WillOnce(SetB());  // Uses the custom action

  Blah blah;
  blah.a = 1;
  blah.b = 2;

  foo.DoBlah(&blah);
  EXPECT_EQ(1, blah.a);
  EXPECT_EQ(77, blah.b);  // The custom action was run

Please reply with more details if this doesn't answer your question. Thanks.

Greg

Mano Nathan

unread,
Oct 22, 2011, 11:37:21 AM10/22/11
to Google C++ Mocking Framework
Hi,
You can use SetArgPointee to set out parameters. This way you don't
have to write custom action. This works very well for base classes,
however it does not work for derived classes. I believe for derived
classes you need to write some custom action as described by Greg.

pseudo code (Sorry, I don't have access to Visual Studio from home)

Blah blah;
blah.a = 1;
blah.b = 2;

EXPECT_CALL(foo, DoBlah(::testing::_)).WillOnce(SetArgPointee(0)
(blah));

Thanks,
Mano

On Oct 21, 6:39 am, Greg Miller <j...@google.com> wrote:
> Hi, Kenneth.
>
> I'm not sure I completely understand what you're describing. It might help
> if you can provide the function signature that you'd like to mock, and a
> description of what you'd like to happen.
>
> My guess, is that you'd like to have an action that given a pointer to a
> class, sets a particular field of that class. One way to do that would be
> with a simple custom
> action<http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Actions...>.
Reply all
Reply to author
Forward
0 new messages