Hello everyone :)
This is a revised/better written version of the question I asked earlier today -- that question is deleted now.
I have a project where I'm getting started with Google Mock. I have created a class, and that class calls functions whithin the Windows API. I've also created a wrapper class with virtual functions wrapping the Windows API, as described in the Google Mock CheatSheet. I'm confused however at how I should pass the wrapper into my class that uses that object. Obviously that object needs to be polymorphic, so I can't pass it by value, forcing me to pass a pointer. That in and of itself is not a problem, but I'm confused as to who should own the pointer to the class wrapping the API.
So... how should I pass the wrapper class into the real class to facilitate mocking?
Here's an example of what I mean:
struct A {
virtual void SomeMethod(int x, int y)
{
::SomeMethod(x, y);
};
};
class Client
{
A * method_;
public:
Client(A * method = new A) : method_(method) {};
void DoSomething()
{
method_->SomeMethod(42, 34);
}
};
struct Mock : public A
{
MOCK_METHOD2(SomeMethod, void(int, int));
};
TEST(MyTest, MyTestWithMock)
{
Mock * mock = new Mock();
EXPECT_CALL(*mock, SomeMethod(42, 34)).Times(1);
Client client(mock); //Should *client* be responsable for deleting mock?
client.DoSomething();
};
EXAMPLE 2:
struct A {
virtual void SomeMethod(int x, int y)
{
::SomeMethod(x, y);
};
};
class Client
{
A * method_;
public:
Client(A * method) : method_(method) {};
static Client Create()
{
static A;
return Client(&A);
}
void DoSomething()
{
method_->SomeMethod(42, 34);
}
};
struct Mock : public A
{
MOCK_METHOD2(SomeMethod, void(int, int));
};
TEST(MyTest, MyTestWithMock)
{
Mock mock;
EXPECT_CALL(mock, SomeMethod(42, 34)).Times(1);
Client client(&mock);
client.DoSomething();
};