几个GTest、GMock的例子

online run

#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;
}

online run turlte

online run window

#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();
// }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值