【问题标题】:gMock for Dummies - How to Getting Started? (Visual-Studio-2019)gMock for Dummies - 如何开始? (Visual-Studio-2019)
【发布时间】:2019-07-31 09:11:09
【问题描述】:

我尝试按照 gMock for Dummies 的入门创建一个非常简单的 Mock 类。

  1. 我在 VS Studio 2019 中新建了一个空白项目

  2. 我按照这个答案 Configure GoogleMock 运行了包管理器 Install-Package gmock -Version 1.8.1

  3. 创建的 packages.config 文件

    <?xml version="1.0" encoding="utf-8"?>`
    <packages>
        <package id="gmock" version="1.8.1" targetFramework="native" />
    </packages>
    
  4. 我创建了 1 个文件 Source.cpp

    class Turtle {
        virtual void PenUp();
    };
    
    void Turtle::PenUp() {
        return;
    }
    
    #include "gmock/gmock.h"
    
    class MockTurtle : public Turtle {
    public:
    
        MOCK_METHOD(void, PenUp, (), (override)); // not working see Pic1
    
        MOCK_METHOD0(PenUp, void()); // not working see Pic2
    
    };
    

第二个例子尝试了类似这个答案How to mock method的语法

图1:

图2:

【问题讨论】:

  • 问题是特定于您的机器/代码。你的例子只是works in both versions。在您没有显示的代码中做了一些不好的事情。
  • 还有一些事情应该在bit different way 中完成。
  • 我将您的代码示例复制并粘贴到我的 Source.cpp 中,Visual Studio 仍然会告诉我“未找到 'MOCK_METHOD' 的函数定义。”除了我展示的 link 的 Source.cpp 和 packages.config 之外,项目中也没有任何内容
  • 澄清一下:这是构建问题还是 IDE 显示的警告?未安装 ReSharper 的 AFAIK Visual Studio IDE 在看到来自 gtest/gmock 的一些宏时会显示一些误报警告。
  • 您的 gMock-for-dummies 链接已过时且已损坏。新链接是google.github.io/googletest/gmock_for_dummies.html

标签: c++ visual-studio googletest visual-studio-2019 googlemock


【解决方案1】:

这里是如何做到这一点,Windows 方式。您需要在此处安装 Windows cmake https://cmake.org/download/。因为 Cygwin 中捆绑的 cmakeVisual Studio 不兼容,所以您需要 Windows 的 cmake.exe,然后从 DOS 终端运行它。我有 cygwin 用于其他事情,但对于所有事情 Visual Studio,它需要在 DOS 终端中以 Window 的方式完成。我正在使用 Visual Studio 2019

git clone https://github.com/google/googletest.git

然后打开 DOS 终端,授予 PATH 已经可以找到cmake.exe。转到您克隆 googletest 存储库的位置。

cd googletest
mkdir c:\opt
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=c:\\opt ..

它将生成通常的googletest-distribution.sln Visual Studio 解决方案文件。现在是使用鼠标的时候了。双击此解决方案文件,它将启动 Visual Studio IDE。构建 ALL_BUILDINSTALLALL_BUILD 将生成 *.lib 文件,INSTALL 将这些文件复制到 c:\opt 位置。在此之后,gtest/gmock 库和头文件应位于C:\opt\ 文件夹中。

【讨论】:

  • 当我按照您的建议构建 googltest - 生成的解决方案构建良好。但我需要一个x86 版本,所以我添加了-A Win32 选项。这样,由于gtest-port.h(2194,20): error C2039: 'int_t': is not a member of 'std',生成的解决方案无法编译。知道如何正确构建 googletest 的 x86 version 吗?
【解决方案2】:

我现在可以编译我的解决方案了:

  1. 我在 VS Studio 2019 中新建了一个空白项目
  2. 我克隆了谷歌仓库googletest
  3. 在项目属性中我添加了 C/C++ 添加。包含目录

    path\to\repo\googlemock
    path\to\repo\googlemock\include
    path\to\repo\googlemock\include\gmock
    path\to\repo\googlemock\include\gmock\internal
    path\to\repo\googletest
    path\to\repo\googletest\include
    path\to\repo\googletest\include\gtest
    path\to\repo\googletest\include\gtest\internal
    
  4. 我创建了 1 个文件 Source.cpp

    #include "gmock/gmock.h"
    #include "gtest/gtest.h"
    
    #include "src/gmock-cardinalities.cc"
    #include "src/gmock-internal-utils.cc"
    #include "src/gmock-matchers.cc"
    #include "src/gmock-spec-builders.cc"
    #include "src/gmock.cc"
    
    #include "src/gtest.cc"
    #include "src/gtest-death-test.cc"
    #include "src/gtest-filepath.cc"
    #include "src/gtest-port.cc"
    #include "src/gtest-printers.cc"
    #include "src/gtest-test-part.cc"
    #include "src/gtest-typed-test.cc"
    
    class Turtle {
    public:
        virtual ~Turtle() {}
        virtual void PenUp() = 0;
    };
    
    class MockTurtle : public Turtle {
    public:
    
        //MOCK_METHOD0(PenUp, void()); // working =)
        MOCK_METHOD(void, PenUp, (), (override)); // working =)
    
    };
    
    int main(int, const char* []) {
        return 0;
    }
    

创意来自this blog post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多