2

Is it possible to mock method that retrieves pointer (or reference) as argument and change pointed object?

I use turtle library - http://turtle.sourceforge.net/ -> A C++ mock object library for Boost. (I know it is not popular library but it can be similar in other libraries).

For example: I need to mock method as:

int f(int* x)
{
    *x = new_value;
    return 0;
}

Next SUT uses x value in code :(

In expactations I can set what my mock returns. But how about to modified argument?

How to do that?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
peter55555
  • 1,413
  • 1
  • 19
  • 36

2 Answers2

4

Take a look at calls and returns actions: http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.expectation.actions

You can create a helper function in your test that modifies x as you want. Pass x to that function.

int function( int* x )
{
    *x = whatever;
    return 0;
}

MOCK_EXPECT(mock->f).calls( &function );

Hope this helps.

attila_s
  • 170
  • 9
0

Fake-It is a simple mocking framework for C++. It supports both GCC and MS Visual C++. Here is how you stub a method and change the pointed object with FakeIt:

struct SomeClass {
    virtual int foo(int * x) = 0;
};

Mock<SomeClass> mock;
When(Method(mock,foo)).AlwaysDo([](int * x) { (*x)++; return 0;});
SomeClass &obj = mock.get();

int num = 0;
ASSERT_EQUAL(0, obj.foo(&num)); // foo should return 0;
ASSERT_EQUAL(1, num);           // num should be 1 after one call to foo; 
Eran Pe'er
  • 511
  • 5
  • 5
  • Thank you for your answer but it's not similar library to turtle. I cannot use other library. Expectation in turtle looks like: MOCK_EXPECT(SomeClass.x) .once() .with(&someValue) .returns(0); – peter55555 Jun 14 '14 at 07:45