Reputation: 13796
I have a test object and I'd like to call EXPECT_CALL
on a method that is not needed in real object, is it possible to mock such undefined new method?
struct MockObject {
MOCK_METHOD2(onRecv, void(void* buffer, size_t len));
};
MockObject
does not inherit from any other class. Is this supported use case of gmock?
Upvotes: 2
Views: 340
Reputation: 1445
To the best of my knowledge, what you have here is well defined behavior. As far as I can tell, all the MOCK_METHOD
macro does is to wire up the method prototype and other elements needed for setting expectations. So while the MOCK_METHOD
family of macros find usage primarily in developing mock classes derived from classes which need testing, your actual usage itself looks sane to me.
While google mocks is neat, you can easily write your own mock methods with very little effort if you find someone complaining about this in a code review.
struct MockObject {
void onRecv(void* buffer, size_t len) {
buf_ = buffer;
len_ = len;
onRecvCallCount_++;
}
void * buf_;
size_t len_;
int onRecvCallCount_ = 0;
};
// actual test setup being
void Setup() {
mockObject.onRecvCallCount_ = 0
}
// The rest of the assertions/expectations get wrapped inside the
// if (mockObject.onRecvCallCount_) { } block
So google mock macros saves all this additional wiring required to set up expectations by providing simple macros like MOCK_METHOD
. So don't hesitate to use it.
Upvotes: 2