GMOCK参数验证

GMOCK Argument Verification

本文关键字:验证 参数 GMOCK      更新时间:2023-10-16

我有一个类,其成员数组类型为int

// Class Defenition
class Foo {
     int array[5];
     // ... Other Memebers
}

拥有另一个具有成员函数的类,该函数具有Foo*类型的参数

class SerialTXInterface {
 public:
    virtual bool print_foo(Foo* strPtr) = 0;
    // ... Other Members
};

上述方法的模型:

MOCK_METHOD1(print_str_s, bool(Array_s<char>* strPtr));

串行TX接口

SerialTXInterface* STX = &SerialTXObject;

Foo对象

Foo FooObj;

函数调用

STX.print_foo(&FooOjb)

如何验证Foo成员数组[5]=={1,2,3,4,5}

这对我有效(如果我将Foo::array公开)

#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace testing;
class Foo {
public:
    int array[5];
    // ... Other Memebers
};
class SerialTXInterface {
public:
    virtual bool print_foo(Foo* strPtr) = 0;
    // ... Other Members
};
class SerialTXMock {
public:
    MOCK_METHOD1(print_foo, bool(Foo* strPtr));
};
TEST(STXUser, Sends12345)
{
    SerialTXMock STXM;
    EXPECT_CALL(STXM, print_foo(Pointee(Field(&Foo::array,ElementsAre(1,2,3,4,    5)))));
    Foo testfoo = {{1,2,3,4,5}};
    STXM.print_foo(&testfoo);
}