googletest - Googlemock - WillOnce 中的多个操作导致构建错误

标签 googletest googlemock

我已经使用 gtest 一段时间了,但最近想尝试一下 gmock。我正在尝试使用返回值的方法模拟类,但也通过引用在输出参数中返回一些东西。这是我的小代码。

#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace ::testing;

class AReal
{
public:
    virtual bool foo(std::vector<int>& v) const = 0;
};

class AMock : public AReal
{
public:
    MOCK_CONST_METHOD1(foo, bool(std::vector<int>&));
};

class B
{
public:
    B(AReal* _a) : a(_a) {}

    bool foo(std::vector<int>& v) const { return a->foo(v); }

private:
    AReal* a;
};

class FooTest : public Test {};

TEST_F(FooTest,
DummyTestVector) {
    AMock a;
    B b(&a);

    std::vector<int> exp = { 1, 2, 3 };
    EXPECT_CALL(a, foo(_))
        .Times(1)
        .WillOnce(AllOf(SetArgReferee<0>(exp), Return(true)));

    std::vector<int> load;
    EXPECT_TRUE(a.foo(load));
    EXPECT_EQ(exp, load);
}

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

但是,这段代码给我这个错误。

$ g++ -Wall -Wextra -std=c++14 -I. -o test test.cpp gmock-gtest-all.cc -lpthread
test.cpp: In member function ‘virtual void FooTest_DummyTestVector_Test::TestBody()’:
test.cpp:40:61: error: no matching function for call to ‘testing::internal::TypedExpectation<bool(std::vector<int>&)>::WillOnce(testing::internal::AllOfResult2<testing::SetArgRefereeActionP<0, std::vector<int> >, testing::internal::ReturnAction<bool> >::type)’
         .WillOnce(AllOf(SetArgReferee<0>(exp), Return(true)));
                                                             ^
In file included from test.cpp:2:0:
gmock/gmock.h:10172:21: note: candidate: testing::internal::TypedExpectation<F>& testing::internal::TypedExpectation<F>::WillOnce(const testing::Action<F>&) [with F = bool(std::vector<int>&)]
   TypedExpectation& WillOnce(const Action<F>& action) {
                     ^
gmock/gmock.h:10172:21: note:   no known conversion for argument 1 from ‘testing::internal::AllOfResult2<testing::SetArgRefereeActionP<0, std::vector<int> >, testing::internal::ReturnAction<bool> >::type {aka testing::internal::BothOfMatcher<testing::SetArgRefereeActionP<0, std::vector<int> >, testing::internal::ReturnAction<bool> >}’ to ‘const testing::Action<bool(std::vector<int>&)>&’

如果我不使用 AllOf 而是只指定一个操作,SetArgRefereeReturn,一切正常。 AllOf 的使用会导致此类错误。我在这里找到了 AllOf gmock multiple in-out parameters SetArgReferee基本上我的代码与答案相同。

最佳答案

在尝试了整个下午之后,我发现这只是我的愚蠢。我一直以某种方式认为 AllOf == DoAll。才意识到这一点。

关于googletest - Googlemock - WillOnce 中的多个操作导致构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672214/

相关文章:

c++ - 链接到多个 .obj 以对控制台应用程序进行单元测试

c++ - 为什么在堆栈对象和堆对象上使用 Times(n) 调用 gmock EXPECT_CALL 时会得到不同的结果?

c++ - 验证并验证 NoMoreInteractions 到 gtest

c++ - 聚合对象使用 googlemock 调用的模拟函数

c++ - RapidJSON::Value&的GoogleTest/Mock SaveArg

c++ - Visual Studio 2013 错误 LNK1120 : 1 unresolved externals and error LNK2019: unresolved external symbol

c++ - 在家庭系统上安装 gtest 时出错

c++ - 如何使用带有 msys 的 mingw 在 Windows 上编译 googletest?

c++ - CMakeLists + gtest

c++ - GTest测试用例 "EXPECT_CALL"编译错误