繁体   English   中英

谷歌模拟链接错误与模拟C函数

[英]google mock link error with mocking C function

我有以下代码,我尝试使用google模拟来模拟C函数:

这是头文件:

A.h

int getValue(int age, int* value, int* number);

这是源文件:

A.c

int getValue(int age, int* value, int* number)
{
    // do something and return value
    return 10;
}

这是源文件bc,用啊

b.c

#include <a.h>

void formValue()
{
   int b = 10;
   int c = 20;
   getValue(1, &b, &c);    
}

这是模拟文件:

AMock.hh

#include "a.h"

class AFileMock {
  public:
    AFileMock();
    ~AFileMock();
    MOCK_METHOD3(getValue, int(int, int *, int *));
};

然后在测试中:

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

#include <b.h>
#include <AFileMock.h>

using testing::_;
using testing::Return;
using testing::InSequence;

class BTest: public testing::Test {

protected:
   AFileMock aFile;

public:
   void SetUp() { }
   void TearDown() { }
};


TEST_F(BTest, test1)
{
   InSequence sequence;
   EXPECT_CALL(aFile, getValue(_, _, _)).
      Times(1);

   formValue();
}

当我尝试运行此测试时,它抱怨说Mock文件中的get link错误,它说:

link error: getValue(_, _, _) is not defined ,它指向Mock文件AMock.hh

但是,如果我在Mock中使用MOCK_CONST_METHOD而不是MOCK_METHOD,它将起作用:

MOCK_CONST_METHOD3(getValue, int(int, int *, int *));

没有编译器错误。

这是什么原因呢?

更换

AMock.hh

#include "a.h"

AMock.hh

extern "C" {
    #include "a.h"
}

原因如下: 在C ++源代码中,外部“ C”的作用是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM