c++ - Google Mock 测试类的真实行为

标签 c++ unit-testing googletest googlemock

我是谷歌测试和谷歌模拟的新手,所以我仍然有点困惑。我只是尝试实现一个带有整数加法、乘法和除法的简单计算器,并为其创建一个遵循真实行为的模拟。我该如何修复它或者我哪里做错了?

你还能向我解释一下我如何确定它是在模拟原始类而不是直接调用原始类吗?先谢谢您了。

这是 CBasicMath.hpp:

#ifndef BASIC_MATH_HPP__
#define BASIC_MATH_HPP__

class CBasicMath
{
public:
    CBasicMath(){}
    virtual ~CBasicMath() {}
    virtual int Addition(int x, int y);
    virtual int Multiply(int x, int y);
    virtual int Divide(int x, int y);
};

#endif //BASIC_MATH_HPP__

这是 CBasicMath.cpp:

#include "CBasicMath.hpp"

int CBasicMath::Addition(int x, int y)
{
   return (x + y);
}

int CBasicMath::Multiply(int x, int y)
{
   return (x * y);
}

int CBasicMath::Divide(int x, int y)
{
   return (x / y);
}

这是mock_basic_test.cpp:

#include "gmock/gmock.h"
#include "CBasicMath.cpp"

using ::testing::_;
using ::testing::AtLeast;
using ::testing::Invoke;

class MockBasicTest : public CBasicMath {
public:
    MockBasicTest() {
    // By default, all calls are delegated to the real object.
    ON_CALL(*this, Addition(_))
        .WillByDefault(Invoke(&real_, &CBasicMath::Addition));
    ON_CALL(*this, Multiply(_))
        .WillByDefault(Invoke(&real_, &CBasicMath::Multiply));
    ON_CALL(*this, Divide(_))
        .WillByDefault(Invoke(&real_, &CBasicMath::Divide));
  }
    MOCK_METHOD2(Addition, int(int x, int y));
    MOCK_METHOD2(Multiply, int(int x, int y));
    MOCK_METHOD2(Divide, int(int x, int y));
private:
    CBasicMath real_;
};

这是 TestBasicMath:

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

class BasicMathTest : public ::testing::Test {
protected:
    BasicMathTest() {}

    virtual ~BasicMathTest() {}

    virtual void SetUp() {
        mTestObj = new CBasicMath();
    }

    virtual void TearDown() {
        delete mTestObj;
    }
    CBasicMath *mTestObj;
};

TEST_F(BasicMathTest, testAddition) {
    MockBasicTest basictest;
    EXPECT_CALL(basictest, Addition(2,3))
        .Times(1);
    EXPECT_EQ(5,basictest.Addition(2,3));
}

TEST_F(BasicMathTest, testMultiply) {
    EXPECT_EQ(6,mTestObj->Multiply(2,3));
}

TEST_F(BasicMathTest, testDivide) {
    EXPECT_EQ(6,mTestObj->Divide(6,1));
}

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

对于所有三个函数,它都会出现如下错误:

In file included from /home/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43:0,
                 from /home/gmock-1.7.0/include/gmock/gmock.h:61,
                 from mock_basic_test.h:1,
                 from TestBasicMath_GoogleTest.cpp:1:
mock_basic_test.h: In constructor ‘MockBasicTest::MockBasicTest()’:
mock_basic_test.h:12:30: error: no matching function for call to ‘MockBasicTest::gmock_Addition(const testing::internal::AnythingMatcher&)’
     ON_CALL(*this, Addition(_))
                             ^
mock_basic_test.h:12:30: note: candidate is:
In file included from /home/gmock-1.7.0/include/gmock/gmock.h:61:0,
                 from mock_basic_test.h:1,
                 from TestBasicMath_GoogleTest.cpp:1:
mock_basic_test.h:19:2: note: testing::internal::MockSpec<int(int, int)>&MockBasicTest::gmock_Addition(const testing::Matcher<int>&, const testing::Matcher<int>&)
  MOCK_METHOD2(Addition, int(int x, int y));
  ^
mock_basic_test.h:19:2: note:   candidate expects 2 arguments, 1 provided

再次感谢您的帮助。

最佳答案

在你的模拟类构造函数代码中

ON_CALL(*this, Addition(_))
    .WillByDefault(Invoke(&real_, &CBasicMath::Addition));

匹配器(_)的数量需要与为函数签名定义的参数数量相同:

ON_CALL(*this, Addition(_,_))
                    //   ^^  Add an additional matcher
    .WillByDefault(Invoke(&real_, &CBasicMath::Addition));

// ...

MOCK_METHOD2(Addition, int(int x, int y));
                        // ^^^^^  ^^^^^ here you have 2 parameters need matching

关于c++ - Google Mock 测试类的真实行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26491246/

相关文章:

java - 是否有适用于 MongoDB 的 DbUnit 替代方案?

c++ - 将测试从 GoogleTest 升级到 GoogleMock (Ubuntu 14) 时出现与 pthread 相关的错误

c++ - 访问内存中的位

c++ - 将临时对象附加到对 const 的引用时出错

java - Spock:setup() cleanup() 方法中的测试名称和结果

unit-testing - FakeItEasy DbSet/IQueryable<T> - Entity Framework 6

c++ - Visual Studio 2019 测试资源管理器没有找到 c++ google 测试

c++ - CMake 添加(测试)可执行文件

c++ - 如何并行执行BIG RGB图像的jpeg编码?

c++ - atof() 返回 float 而不是 double