当前位置:网站首页>几个GTest、GMock的例子
几个GTest、GMock的例子
2022-07-30 18:35:00 【一只公羊】
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class FooInterface {
public:
virtual ~FooInterface() {
//析构函数必须是virtual
}
virtual std::string getArbitraryString() = 0;
virtual int getPosition(int n) = 0;
virtual void setValue(std::string& value){
};
virtual void setDoubleValues(int x, int y){
};
};
class MockFoo : public FooInterface {
// MockFoo类继承 FooInterface
public:
MOCK_METHOD0(getArbitraryString,std::string());
MOCK_METHOD1(getPosition,int(int));
MOCK_METHOD1(setValue, void(std::string& value));
MOCK_METHOD2(setDoubleValues, void(int x, int y));
};
TEST(TestField, foo) {
MockFoo mockFoo; //声明一个MockFoo的对象:mockFoo
//定义一个期望行为,其中Times(1)的意思是运行一次,WillOnce(Return(value))的意思是第一次运行时把value作为getArbitraryString()方法的返回值。
EXPECT_CALL(mockFoo, getArbitraryString())
.Times(1)
.WillOnce(::testing::Return("Hello World!"));
std::string returnValue = mockFoo.getArbitraryString();
// std::cout << "Returned Value: " << returnValue << std::endl;
//在这里Times(2)意思是调用两次,但是下边只调用了一次,所以会报出异常
EXPECT_CALL(mockFoo, getPosition(1))
.Times(::testing::AtLeast(1))
.WillOnce(::testing::Return(100))
.WillRepeatedly(::testing::Return(200));
int val = mockFoo.getPosition(1); // 100
val = mockFoo.getPosition(1); // 100
val = mockFoo.getPosition(1); // 100
val = mockFoo.getPosition(1); // 100
// std::cout << "Returned Value: " << val << std::endl;
}
#include <iostream>
#include <stdexcept>
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
namespace windows {
enum class Severity {
LOW, MEDIUM, CRITICAL };
bool troubleshooting_tool(Severity severity)
{
std::cout << "Looking for solution... \n";
std::this_thread::sleep_for(std::chrono::seconds(2000));
throw std::runtime_error {
"System Windows cannot find the solution."};
}
} // namespace windows
struct AdvancedTroubleshootingTool {
void FixMonitorDriver()
{
try {
predicate(windows::Severity::CRITICAL);
} catch (const std::exception& ex)
{
std::cout << "Success: " << ex.what() << "\n";
throw;
}
}
std::function<bool(windows::Severity)> predicate = &windows::troubleshooting_tool;
};
TEST(AdvancedTroubleshootingToolTest, DoesNotFixMonitorDriver)
{
::testing::MockFunction<bool(windows::Severity)> windowsTrouleshootingMock{
};
EXPECT_CALL(windowsTrouleshootingMock, Call(windows::Severity::CRITICAL))
.WillOnce(::testing::Throw(std::runtime_error{
"Exception thrown from mock."}));
AdvancedTroubleshootingTool unit_under_test{
windowsTrouleshootingMock.AsStdFunction() };
EXPECT_THROW(unit_under_test.FixMonitorDriver(), std::exception);
}
// int main (int argc, char * argv[])
// {
// ::testing::InitGoogleTest(&argc, argv);
// return RUN_ALL_TESTS();
// }
边栏推荐
猜你喜欢

Hello, my new name is "Bronze Lock/Tongsuo"

A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....

Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents

Scrapy框架介绍

Confluence OGNL注入漏洞复现(CVE-2022-26134)

The sixteenth issue of eight-part article Balabala said (MQ)

Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!

荐号 | 对你有恩的人,不要请吃饭来报答

生物医学论文有何价值 论文中译英怎样翻译效果好

ESP8266-Arduino编程实例-DS18B20温度传感器驱动
随机推荐
【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显
怎么样的框架对于开发者是友好的?
单例模式 (Singleton)
经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法
DM8:单库单实例搭建本地数据守护服务
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
The use of terminal split screen tool Terminalx
【Qt Designer工具的使用】
Pytorch foundation -- tensorboard use (1)
终端分屏工具Terminalx的使用
kotlin by lazy
Pytorch基础--tensorboard使用(一)
【Prometheus】Prometheus联邦的一次优化记录[续]
ctf.show_web5
微信小程序云开发 | 城市信息管理
core sound driver详解
OneFlow源码解析:Op、Kernel与解释器
Node encapsulates a console progress bar plugin
The sixteenth issue of eight-part article Balabala said (MQ)