gmock:在每次模拟调用中返回不同的值
gmock: Returns distinct values on each mock invocation
给定:
struct MockThis : public IMocker {
MOCK_METHOD0(ReturnThis, std::string());
};
我在这个对象上设置了一个ON_CALL,所以它会返回一个默认值,但假设我希望ReturnThis在我的特定测试中为每次调用它返回一个不同的字符串,我该如何做到这一点?
您可以考虑这样做:
struct MockThis : public IMocker {
MOCK_METHOD0(ReturnThis, std::string());
MockThis() {
ON_CALL(*this, ReturnThis())
.WillByDefault(Invoke(&real_, &MockThis ::ReturnStringVariations));
}
protected:
static std::string randomStrings[10];
std::string ReturnStringVariations() {
// Return some string on either random conditions, or calls counted, etc.
unsigned int strIndex = std::rand() % 10;
return randomStrings[strIndex];
}
};
std::string MockThis::randomStrings[10] = {
"Random string 1" ,
// ...
"Random string 10"
};
对于从预定义的数组(如上所示(、格式化的变体或其他类型生成字符串,您可能会考虑使用c++11伪随机数功能中的一些东西。
相关文章:
- 从python中调用C++函数并获取返回值
- 返回递归调用和仅递归调用的区别
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 调用CreateProcess()并获取字符串的返回值
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 返回指向对象的指针的函数调用是否为 prvalue?
- 如何安全地从 DLL 调用返回对象
- getopt_long_only第二次调用时返回 -1
- 如何构造可以调用和返回两种不同类型的模板
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- C++如何使函数返回调用它的对象
- 谷歌嘲笑!我的测试出错.默认情况下,呼叫将返回调用
- 仅使用有关类型而不是对象的信息返回调用的类型
- 从C#返回调用C++dll函数的值时发生访问冲突
- 问题5.3:访问/返回/调用动态创建的复选框(?)
- 按值返回调用复制构造函数或复制赋值运算符
- Tcl_Eval在出错时返回调用堆栈
- 按值返回调用复制构造函数
- 为什么在没有显式返回语句的情况下,递归返回调用会跳出堆栈