如何仅覆盖一个EXPECT_CALL的默认ON_CALL操作,并在以后返回到默认操作

How to override the default ON_CALL action for just one EXPECT_CALL and go back to the default action later

本文关键字:默认 操作 CALL 何仅 返回 ON 覆盖 一个 EXPECT      更新时间:2023-10-16

我想测试我的系统的方法,其返回值部分取决于对某种连接接口的调用的返回值。在大多数情况下,我希望IConnection在对它的open(_, _)方法进行任何类型的调用时返回true。除了在一种情况下,当我显式测试连接失败的条件时。

例:

/*
* Some kind of network interface with method `open`
*/
class IConnection {
public:
IConnection() = default;
virtual ~IConnection() = default;
virtual bool open(const std::string& address, int port) = 0;
};
class ConnectionMock: public IConnection {
public:
MOCK_METHOD2(open, bool(const std::string& address, int port));
};
class MySystem {
public:
MySystem() = delete;
MySystem(std::shared_ptr<IConnection> connection): connection_(connection) {}
bool doSth() {
/*
* Do some things, but fail if connection fails
*/
bool connectionStatus = connection_->open("127.0.0.1", 6969);
if (!connectionStatus) {
return false;
}
// do other things
return true;
}
private:
std::shared_ptr<IConnection> connection_;
};
TEST(MySystemShould, returnFalseIfFailedToOpenConnectionAndTrueIfSucceeded) {
auto connectionMock = std::make_shared<NiceMock<ConnectionMock> >();
ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));
MySystem system(connectionMock);
// if I don't specify Times test fill fail, because WillOnce automatically sets Times(1)
EXPECT_CALL(*connectionMock, open(_, _)).Times(AnyNumber()).WillOnce(Return(false));
/*
* Commented code below is not a good solution - after expectation retires
* the test will fail upon subsequent calls
*/
//EXPECT_CALL(*connectionMock, open(_, _)).WillOnce(Return(false)).RetiresOnSaturation();
ASSERT_FALSE(system.doSth());
/*
* Code bellow allows me to avoid the warning
*/
//EXPECT_CALL(*connectionMock, open(_, _)).WillRepeatedly(Return(true));
ASSERT_TRUE(system.doSth());
}

我当前解决方案的问题在于,当EXPECT_CALL覆盖饱和时,即使 gmock 返回到ON_CALL上指定的默认操作,每次后续对open(_, _)的调用都会导致以下警告:

GMOCK WARNING:
/whatever.cpp:105: Actions ran out in EXPECT_CALL(*connectionMock, open(_, _))...
Called 2 times, but only 1 WillOnce() is specified - taking default action specified at:
/whatever.cpp:103:

即使我正在使用NiceMock.我可以通过用WillRepeatedly(Return(true))指定EXPECT_CALL来摆脱警告,但这是ON_CALL中我的代码重复。

我想知道,如何覆盖使用ON_CALL指定的默认操作,只调用一次IConnection::open,然后返回到默认值,而不会导致 gmock 打印警告。完美的解决方案类似于:

EXPECT_CALL(*connectionMock, open(_, _)).WillOnce(Return(false)).DisableExpectationAfterSaturation();

但它不存在。RetiresOnSaturation无法按我的意愿工作,因为它在饱和后无法通过测试(与ON_CALL指定的操作不匹配(。

编辑 2
DoDefault()- 功能接近问题中提出的内容。它指定EXPECT_CALL中的操作应返回到ON_CALL指定的默认操作:

using ::testing::DoDefault;
// Default action
ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));
// returns true once and then goes back to the default action
EXPECT_CALL(*connectionMock, open(_, _)
.WillOnce(Return(false))
.WillRepeatedly(DoDefault());

初步答案

如果IConnection::open的返回值取决于参数,则可以指定两次ON_CALL但使用不同的参数(或者更确切地说是参数而不是占位符(:

ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));
ON_CALL(*connectionMock, open("BAD_ADDRESS", 20)).WillByDefault(Return(false));

因此,每当使用参数"BAD_ADDRESS"和 20 调用模拟方法open时,它将返回 false,否则返回 true。

下面是一个简单的示例:

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
class A {
public:
virtual bool bla(int a) = 0;
};
class MOCKA : public A {
public:
MOCK_METHOD1(bla, bool(int));
};
TEST(ABC, aBABA) {
MOCKA a;
ON_CALL(a, bla(_)).WillByDefault(Return(false));
ON_CALL(a, bla(1)).WillByDefault(Return(true));
EXPECT_CALL(a, bla(_)).Times(AnyNumber());
EXPECT_TRUE(a.bla(1));
EXPECT_TRUE(a.bla(1));
EXPECT_TRUE(a.bla(1));
EXPECT_FALSE(a.bla(2));
EXPECT_FALSE(a.bla(3));
EXPECT_FALSE(a.bla(4));
}

编辑 1我想现在我明白了问题,如果我明白了,那么解决方案非常简单:

EXPECT_CALL(*connectionMock, open(_, _))
.Times(AnyNumber())
.WillOnce(Return(true))
.WillRepeatedly(Return(false));

ConnectionMock::openMySystem::doSth内部调用时,它将返回一次true然后始终返回false无论参数是什么。在这种情况下,您也不需要指定ON_CALL。还是您肯定需要使用ON_CALL而不是EXPECT_CALL来指定操作?

相关文章: