Skip to content

Instantly share code, notes, and snippets.

@blogdarkspot
Created July 18, 2017 01:08
Show Gist options
  • Save blogdarkspot/7ce44306dfb3b8630839fba00a1eddcb to your computer and use it in GitHub Desktop.
Save blogdarkspot/7ce44306dfb3b8630839fba00a1eddcb to your computer and use it in GitHub Desktop.
using ::testing::_;
using ::testing::Invoke;
class Foo {
public:
virtual ~Foo() {}
virtual char DoThis(int n) = 0;
virtual void DoThat(const char* s, int* p) = 0;
};
class FakeFoo : public Foo {
public:
virtual char DoThis(int n) {
return (n > 0) ? '+' :
(n < 0) ? '-' : '0';
}
virtual void DoThat(const char* s, int* p) {
*p = strlen(s);
}
};
class MockFoo : public Foo {
public:
// Normal mock method definitions using Google Mock.
MOCK_METHOD1(DoThis, char(int n));
MOCK_METHOD2(DoThat, void(const char* s, int* p));
// Delegates the default actions of the methods to a FakeFoo object.
// This must be called *before* the custom ON_CALL() statements.
void DelegateToFake() {
ON_CALL(*this, DoThis(_))
.WillByDefault(Invoke(&fake_, &FakeFoo::DoThis));
ON_CALL(*this, DoThat(_, _))
.WillByDefault(Invoke(&fake_, &FakeFoo::DoThat));
}
private:
FakeFoo fake_; // Keeps an instance of the fake in the mock.
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment