如何使用谷歌测试零或一个函数调用进行检查
How to check with Google Test zero or one function call?
我想写一个测试,在不同的线程中调用两个函数,我希望根据哪个函数首先工作,会出现以下情况:
EXPECT_CALL(foo, bar(arg_1));
或者这个:
EXPECT_CALL(foo, bar(arg_1)).RetiresOnSaturation();
EXPECT_CALL(foo, bar(arg_2)).RetiresOnSaturation();
EXPECT_CALL(foo, bar(arg_1)).RetiresOnSaturation();
正是按照这个顺序。最简单的方法是什么?我是谷歌测试的新手。
由于您不能执行模式或条件期望中的任何一个,请仅使用可选期望,将固定期望设置为arg1,将可选(次数(AtMost(1)))期望设置为arg2,按顺序排列,并使可选期望加上第三个期望。
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class Foo {
public:
MOCK_METHOD1(bar, void(int));
};
class ExpectCall1
{
public:
ExpectCall1(Foo& foo) : foo_(foo) {}
void operator()()
{
EXPECT_CALL(foo_, bar(1)).Times(1);
}
private:
Foo& foo_;
};
class Demo : public ::testing::Test
{
virtual void SetUp()
{
::testing::InSequence dummy;
EXPECT_CALL(foo_, bar(1)).Times(1);
EXPECT_CALL(foo_, bar(2)).Times(::testing::AtMost(1)).
WillOnce(::testing::InvokeWithoutArgs(ExpectCall1(foo_)));
}
protected:
Foo foo_;
};
TEST_F(Demo, Success1)
{
foo_.bar(1);
}
TEST_F(Demo, Success2)
{
foo_.bar(1);
foo_.bar(2);
foo_.bar(1);
}
TEST_F(Demo, Fail1)
{
foo_.bar(1);
foo_.bar(1);
}
TEST_F(Demo, Fail2)
{
foo_.bar(1);
foo_.bar(2);
}
TEST_F(Demo, Fail3)
{
foo_.bar(1);
foo_.bar(1);
foo_.bar(2);
}
相关文章:
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 重载运算符主体仅包含一个函数调用
- 在 MySQL 连接器C++ API 中使用一个函数调用执行多个查询的正确方法是什么?
- C++编译器在一个源文件中的一个函数调用中引发错误,但在具有相同函数调用的另一个源文件中不会引发错误
- while 循环在一个函数调用中执行两次
- 在C++中嵌入Python:在Python脚本中导入模块在一个函数调用过程中有效,但在另一个调用过程中无效
- 如何从前面定义的另一个函数调用函数模板
- 下面的代码会被优化为一个函数调用吗
- C++中的神秘:函数调用行为不端只是因为存在另一个函数调用,即使它们是不相关的
- 我可以静态地阻止一个函数调用另一个函数吗
- c++oop初学者-在一个函数调用中返回向量中每个创建对象的输出和
- 如何告诉编译器向编译的每个函数添加一个函数调用
- 存储一个函数的迭代结果,并在被另一个函数调用时使用它们
- 在一个函数调用中创建线程池,并从另一个函数调用来使用它
- 如何使用谷歌测试零或一个函数调用进行检查
- 一个函数调用另一个函数时使用的内存会发生什么情况
- 从一个函数调用另一个函数
- 只需一个函数调用即可输出分数
- 在C/ c++中,当两个函数具有相同的名称,一个函数调用另一个函数时,它将工作
- 为什么arduino忽略了一个函数调用