如何仅覆盖一个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
我想测试我的系统的方法,其返回值部分取决于对某种连接接口的调用的返回值。在大多数情况下,我希望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::open
在MySystem::doSth
内部调用时,它将返回一次true
然后始终返回false
无论参数是什么。在这种情况下,您也不需要指定ON_CALL
。还是您肯定需要使用ON_CALL
而不是EXPECT_CALL
来指定操作?
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何使用默认参数等选择模板专业化
- 我什么时候会默认(而不是删除)基类中的复制和移动操作
- 这将执行默认移动操作吗?
- 如何对移动操作进行单元测试(默认)?
- 如果普通默认构造函数不执行任何操作,为什么我们不能使用 malloc 创建平凡可构造的对象?
- 为什么 if (a == b || c) && (d != 0) {操作 1} 默认运行,但 (a == e || f ) && (d!= 0) {op2} 被忽略?
- 如何仅覆盖一个EXPECT_CALL的默认ON_CALL操作,并在以后返回到默认操作
- 如果默认构造函数不执行任何操作,则目的是什么
- 在集合上运行的函数模板,该集合具有默认的"transform"函数,该函数不执行任何操作
- gmock 设置默认操作 / ON_CALL vs. EXPECT_CALL
- 如何编写两个初始化操作(一个作为默认初始化,另一个作为用户输入)?两者都是类的构造函数C++
- 在新的 c++11 项目中,我是否应该默认删除任何操作
- 模板类=默认类不执行任何操作
- 是否可以使函数模板对不适当的类型而不是错误执行默认操作
- c++ 的默认分配操作行为是什么?
- 不理解 Stroustup 先生的建议,删除复制默认值并为抽象类 Shape 移动操作
- 默认的虚拟析构函数是否阻止编译器生成的移动操作
- 构造导致运算符对默认构造对象进行操作