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

Why do I get different results when calling gmock EXPECT_CALL with Times(n) on stack objects and heap objects?

我想在我的代码中将 gmock EXPECT_CALL 与 Times(n) 一起使用。我写了一个示例测试,在调用使用 new 关键字创建的 对象时 得到了错误的结果 但它准确地处理堆栈对象。
因为我打算在实际测试中使用堆对象,所以我需要知道我在这里做错了什么。
这是我的示例代码。

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

class Point
{
private:
    int x;
    int y;

public:
    Point(int a, int b)
    {
        this->x = a;
        this->y = b;
    }

    virtual int getSum()
    {
        return x + y;
    }
};

class MockPoint :
        public Point
{
public:
    MockPoint(int a, int b):Point(a,b){}

    MOCK_METHOD0(getSum, int());
};

class PointTests :
        public ::testing::Test
{
};

TEST_F(PointTests, objectTest)
{
    MockPoint p(10, 20);
    EXPECT_CALL(p, getSum()).Times(10);
    p.getSum();
}

TEST_F(PointTests, pointerTest)
{
    MockPoint* p = new MockPoint(10,20);
    EXPECT_CALL(*p, getSum()).Times(10);
    p->getSum();
}

我希望这两项测试都失败,因为我只调用了一次 getSum()。

但这是我在 运行 测试时实际得到的结果。

[ RUN      ] PointTests.objectTest
/home/lasantha/test/PointTests.cpp:44: Failure
Actual function call count doesn't match EXPECT_CALL(p, getSum())...
         Expected: to be called 10 times
           Actual: called once - unsatisfied and active
[  FAILED  ] PointTests.objectTest (0 ms)
[ RUN      ] PointTests.pointerTest
[       OK ] PointTests.pointerTest (0 ms)
[----------] 2 tests from PointTests (0 ms total)

您必须删除 MockPoint 才能检查条件:

TEST_F(PointTests, pointerTest)
{
    MockPoint* p = new MockPoint(10,20);
    EXPECT_CALL(*p, getSum()).Times(10);
    p->getSum();
    **delete(p);**
}