Created
May 6, 2017 08:31
-
-
Save jsrois/1974a5bf7c97dfdaa70ef4ecacca43fc to your computer and use it in GitHub Desktop.
GoogleMock adding a default action to increment a counter using lambdas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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