GMOCK 参数验证

GMOCK Argument Verification

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

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

有另一个 class 的成员函数,其参数类型为 Foo*

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

模拟上述方法:

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

SerialTX 接口

SerialTXInterface* STX = &SerialTXObject;

Foo 对象

Foo FooObj;

函数调用

STX.print_foo(&FooOjb)

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

这对我有用(如果我使 Foo::array public)

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