c++ - GMOCK 参数验证

标签 c++ googlemock

我有一个类,成员数组类型为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));

SerialTX接口(interface)

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

关于c++ - GMOCK 参数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440328/

相关文章:

c++ - 关联容器引用

c++ - 网格划分和快速查找

c++ - 可变参数推导指南不是由 g++ 采用,由 clang++ 采用 - 谁是正确的?

c++ - mock_method 上的 gmock 编译错误(在 testing::internal::FunctionMocker 中)

C++ 自定义断言

c++ - 无法从文本文件中读取并存储到字符串中

c++ - 谷歌模拟 expect_call 取消另一个期望

c++ - 如何使用签名 `object ()` 模拟函数

c++ - 谷歌测试返回值

C++ gmock - 将空指针传递给 SaveArg 时会发生什么