In Mockito we can specify multiple returns like (taken from here):
//you can set different behavior for consecutive method calls.
//Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls.
when(mock.someMethod("some arg"))
.thenReturn(new RuntimeException())
.thenReturn("foo");
//There is a shorter way of consecutive stubbing:
when(mock.someMethod()).thenReturn(1,2,3);
when(mock.otherMethod()).thenThrow(exc1, exc2);
Is there a way to specify multiple returns for a mock made with gmock? Currently I have:
store_mock_ = std::make_shared<StorageMock>();
ON_CALL(*store_mock_, getFileName(_)).Return("file1").Return("file2");
which doesn't compile because I can't figure out multiple returns in gmock. Is this possible with gmock? If not, is there another way to solve this problem? I've found that we can EXPECT
multiple return values like:
using ::testing::Return;...
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(100))
.WillOnce(Return(200))
.WillOnce(Return(300));
However, I haven't found any docs for mocking multiple returns with ON_CALL
.
IncrementDummy
example is really helpful. Kinda sucks that the best way to define multiple returns is so involved. :( – Ouster