Skip to content

Instantly share code, notes, and snippets.

@jsrois
Created May 6, 2017 08:31
Show Gist options
  • Save jsrois/1974a5bf7c97dfdaa70ef4ecacca43fc to your computer and use it in GitHub Desktop.
Save jsrois/1974a5bf7c97dfdaa70ef4ecacca43fc to your computer and use it in GitHub Desktop.
GoogleMock adding a default action to increment a counter using lambdas.
#include <gmock/gmock.h>
using namespace testing;
struct SomeStruct {
virtual void apply() {
// do nothing
}
};
struct MockStruct : public SomeStruct {
MOCK_METHOD0(apply,void());
};
void func(SomeStruct& s) {
s.apply();
}
TEST(StructTest, do_your_magic) {
MockStruct mock;
int callCounter = 0;
ON_CALL(mock,apply())
.WillByDefault(InvokeWithoutArgs([&callCounter](){
callCounter++;
}));
func(mock);
ASSERT_THAT(callCounter,Eq(1));
}
int main(int argc, char**argv) {
::testing::InitGoogleMock(&argc,argv);
return RUN_ALL_TESTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment