Gmock在实际函数调用计数上不匹配EXPECT_CALL

时间:2018-10-26 05:40:00

标签: unit-testing googletest googlemock gmock

我是使用Google Mock的新手,但遇到错误:“错误:实际函数调用计数与EXPECT_CALL(…)不匹配。”

我不确定我做错了什么。我按照建议的here提出了此解决方案,但仍然遇到此错误。

这是我正在处理的示例代码:

原始班级

//orig.h
#include <stdio.h>

class Orig {
public:
    virtual ~Orig(){}
    virtual int this_func(uint8_t x, uint8_t* y, uint8_t z) = 0;
    virtual int that_func(uint8_t x, uint8_t* y, uint8_t z) = 0;
    virtual void those_func(int abc) = 0;
}; 

模拟原始类

//mock_orig.h
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "calling_class.h"

class MockOrig : public Orig {
public:
    MOCK_METHOD3(this_func, int(uint8_t x, uint8_t* y, uint8_t z));
    MOCK_METHOD3(that_func, int(uint8_t x, uint8_t* y, uint8_t z));
    MOCK_METHOD1(those_func, void(int abc));
};

呼叫班

//calling_class.h
#define MY_CONST1       0x01
#define MY_CONST2       0x02
#define X_CONST1        0x03
#define X_CONST2        0x04
#define SAMPLE_VALUE    (20)
#define TOTAL_VALUE     (100/SAMPLE_VALUE)

#include "orig.h"

class CallingClass {
public:
    explicit CallingClass(Orig * o) : mOrig(o){};
    void calling_func(void) {
        static int oldValue = 0;
        static uint16_t counter = 0;
        uint8_t buffer[2] = {0};
        int firstBit = 0;
        int secondBit = 0;

        if (0 == oldValue && (0 == mOrig->this_func(X_CONST1, buffer, 1))
        {
            secondBit = (int)(buffer[0] & MY_CONST2);
            firstBit = (int)(buffer[0] & MY_CONST1);
            if((oldValue != secondBit) && (0 != firstBit))
            {
                mOrig->those_func(secondBit);
                oldValue = secondBit;
                counter = TOTAL_VALUE;
            }
            return;
        }

        if (0 >= --counter && (0 == mOrig->this_func(X_CONST1, buffer, 1)))
        {
            uint8_t out = buffer[0] | MY_CONST2;
            mOrig->that_func(X_CONST2, &out, 1);
            oldValue = 0;
        }
        return;
    }

    private:
        Orig * mOrig;
}

正确测试

//mock_orig.cpp
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <memory>

TEST(myTest, testMyFunction)
{
    std::shared_ptr<Orig> mO(new MockOrig);
    uint8_t buffer[1];
    EXPECT_CALL(*std::static_pointer_cast<MockOrig>(mO), this_func(X_CONST1, buffer, 1))
        .Times(1);
     CallingClass callerClass(mO.get());
     callerClass.calling_func();

}

int main(int argc, char **argv)
{
    testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

有人可以告诉我这还有什么问题吗?任何帮助/线索将不胜感激。谢谢!

0 个答案:

没有答案