如何在gmock中指定连续的returns?
How to specify consecutive returns in gmock?
在 Mockito 中,我们可以指定多个 return,例如(取自 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);
有没有办法为使用 gmock 制作的模拟指定多个 return?目前我有:
store_mock_ = std::make_shared<StorageMock>();
ON_CALL(*store_mock_, getFileName(_)).Return("file1").Return("file2");
无法编译,因为我无法在 gmock 中找出多个 return。这可能与 gmock 吗?如果没有,是否有另一种方法可以解决这个问题?我发现我们可以 EXPECT
多个 return 值,例如:
using ::testing::Return;...
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(100))
.WillOnce(Return(200))
.WillOnce(Return(300));
但是,我还没有找到任何使用 ON_CALL
模拟多个 return 的文档。
ON_CALL
更多用于设置函数的默认行为。 IE。你知道在测试代码中调用模拟函数,你想设置一些默认值,但函数被调用多少次实际上并不重要。
ON_CALL(foo, Sign(_))
.WillByDefault(Return(-1));
ON_CALL(foo, Sign(0))
.WillByDefault(Return(0));
ON_CALL(foo, Sign(Gt(0)))
.WillByDefault(Return(1));
为了得到你想要的行为,我会使用 expectations - 你已经提供了一些有问题的例子,只是为了展示更多 - 一个你期望 1
, 2
然后的例子总是 3
:
EXPECT_CALL(foo, Sign(_))
.WillOnce(Return(1))
.WillOnce(Return(2))
.WillRepeatedly(Return(3));
EXPECT_CALL
"way" 当你想在测试夹具 SetUp
中设置它时可能会很麻烦 - 有些测试可能只调用一次 foo
。但是,当然,有一些方法可以为后续调用提供 "control" ON_CALL
return 值 - 但你必须通过特殊操作来完成 - 例如获取某些函数的结果 - 如本例所示:
class IDummy
{
public:
virtual int foo() = 0;
};
class DummyMock : public IDummy
{
public:
MOCK_METHOD0(foo, int());
};
using namespace ::testing;
class DummyTestSuite : public Test
{
protected:
DummyMock dummy;
void SetUp() override
{
ON_CALL(dummy, foo())
.WillByDefault(
InvokeWithoutArgs(this, &DummyTestSuite::IncrementDummy));
}
int dummyValue = 0;
int IncrementDummy()
{
return ++dummyValue;
}
};
TEST_F(DummyTestSuite, aaa)
{
ASSERT_EQ(1, dummy.foo());
ASSERT_EQ(2, dummy.foo());
ASSERT_EQ(3, dummy.foo());
}
@PiotrNycz 的回答是正确的,也是首选方案。
通过 lambda 函数的替代方法可能 给你更多的灵活性:
uint32_t callCount = 0;
ON_CALL(*turtle, GetX())
.WillByDefault(testing::Invoke(
[&callCount]() -> int {
return ++callCount * 100;
}
));
在 Mockito 中,我们可以指定多个 return,例如(取自 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);
有没有办法为使用 gmock 制作的模拟指定多个 return?目前我有:
store_mock_ = std::make_shared<StorageMock>();
ON_CALL(*store_mock_, getFileName(_)).Return("file1").Return("file2");
无法编译,因为我无法在 gmock 中找出多个 return。这可能与 gmock 吗?如果没有,是否有另一种方法可以解决这个问题?我发现我们可以 EXPECT
多个 return 值,例如:
using ::testing::Return;...
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(100))
.WillOnce(Return(200))
.WillOnce(Return(300));
但是,我还没有找到任何使用 ON_CALL
模拟多个 return 的文档。
ON_CALL
更多用于设置函数的默认行为。 IE。你知道在测试代码中调用模拟函数,你想设置一些默认值,但函数被调用多少次实际上并不重要。
ON_CALL(foo, Sign(_))
.WillByDefault(Return(-1));
ON_CALL(foo, Sign(0))
.WillByDefault(Return(0));
ON_CALL(foo, Sign(Gt(0)))
.WillByDefault(Return(1));
为了得到你想要的行为,我会使用 expectations - 你已经提供了一些有问题的例子,只是为了展示更多 - 一个你期望 1
, 2
然后的例子总是 3
:
EXPECT_CALL(foo, Sign(_))
.WillOnce(Return(1))
.WillOnce(Return(2))
.WillRepeatedly(Return(3));
EXPECT_CALL
"way" 当你想在测试夹具 SetUp
中设置它时可能会很麻烦 - 有些测试可能只调用一次 foo
。但是,当然,有一些方法可以为后续调用提供 "control" ON_CALL
return 值 - 但你必须通过特殊操作来完成 - 例如获取某些函数的结果 - 如本例所示:
class IDummy
{
public:
virtual int foo() = 0;
};
class DummyMock : public IDummy
{
public:
MOCK_METHOD0(foo, int());
};
using namespace ::testing;
class DummyTestSuite : public Test
{
protected:
DummyMock dummy;
void SetUp() override
{
ON_CALL(dummy, foo())
.WillByDefault(
InvokeWithoutArgs(this, &DummyTestSuite::IncrementDummy));
}
int dummyValue = 0;
int IncrementDummy()
{
return ++dummyValue;
}
};
TEST_F(DummyTestSuite, aaa)
{
ASSERT_EQ(1, dummy.foo());
ASSERT_EQ(2, dummy.foo());
ASSERT_EQ(3, dummy.foo());
}
@PiotrNycz 的回答是正确的,也是首选方案。
通过 lambda 函数的替代方法可能 给你更多的灵活性:
uint32_t callCount = 0;
ON_CALL(*turtle, GetX())
.WillByDefault(testing::Invoke(
[&callCount]() -> int {
return ++callCount * 100;
}
));