我使用GTest遇到了无法理解的行为。
问题很容易。我在叫sut方法。该方法调用StrictMock对象方法。我对该方法调用没有任何期望。根据GTest规范,此类测试应因“无趣的模拟函数调用”而失败。实际上不是。
这里是代码:
汽车

#pragma once

#include <iostream>
#include <memory>

class EngineIfc;

class Car
{
public:
    Car(const std::shared_ptr<EngineIfc> & engine);

    void start();
    void stop();

private:
    std::shared_ptr<EngineIfc> engine;
};
Car.cpp
#include "Car.hpp"
#include "Engine.hpp"

Car::Car(const std::shared_ptr<EngineIfc> & engine) : engine(engine) {}

void Car::start()
{
    std::cout << "Start Car" << std::endl;
    engine->start();
}

void Car::stop()
{
    std::cout << "Stop Car" << std::endl;
    engine->stop();
}
EngineIfc.hpp
#pragma once

class EngineIfc
{
public:
    virtual ~EngineIfc() = default;
    virtual void start() = 0;
    virtual void stop() = 0;
};
Engine.hpp
#pragma once

#include "EngineIfc.hpp"

class Engine : public EngineIfc
{
public:
    void start() override;
    void stop() override;
};
Engine.cpp
#include "Engine.hpp"

#include <iostream>

void Engine::start()
{
    std::cout << "Start Engine" << std::endl;
}

void Engine::stop()
{
    std::cout << "Stop Engine" << std::endl;
}
EngineMock.hpp
#pragma once

#include "../EngineIfc.hpp"

#include "gmock/gmock.h"

class EngineMock : public EngineIfc
{
public:
    MOCK_METHOD0(start, void());
    MOCK_METHOD0(stop, void());
};
CarTestSuite.cpp
#include "EngineMock.hpp"
#include "../Car.hpp"

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

struct CarTestSuite : testing::Test
{
    CarTestSuite() : engine(std::make_shared<EngineMock>()), car(engine)
    {}

    testing::StrictMock<std::shared_ptr<EngineMock>> engine;
    Car car;
};

TEST_F(CarTestSuite, testWhereUnexpectedStrictMockFunctionCallDoesNotFailTheTest)
{
    car.start();
    car.stop();
}
控制台的输出:
Running main() from /Users/daniel/Desktop/TimeStamp/GoogleTest/lib/googletest/src/gtest_main.cc
Start Car

GMOCK WARNING:
Uninteresting mock function call - returning directly.
    Function call: start()
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
Stop Car

GMOCK WARNING:
Uninteresting mock function call - returning directly.
    Function call: stop()
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
我试图更改engine定义和这样的声明:
struct CarTestSuite : testing::Test
{
    CarTestSuite() : engine(std::make_shared<testing::StrictMock<EngineMock>>()), car(engine)
    {}

    std::shared_ptr<testing::StrictMock<EngineMock>> engine;
    Car car;
};
之后,测试将按预期失败。
问题是:为什么?
请帮助我理解。
提前致谢。

最佳答案

我认为这是由于StrictMock(以及NiceMockNaggyMock,是默认的)是这样的事实:

template <class MockClass>
class StrictMock : public MockClass
[...]
因此,在testing::StrictMock<std::shared_ptr<EngineMock>>情况下,StrictMock的基类是shared_ptr<EngineMock>,而不是EngineMock本身。根据the source file中的注释:A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>, and StrictMock<MockFoo> only works for mock methods defined using the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.所以在这种情况下,我认为不是直接在MockFoo类中-因此它可能无法正常工作。

关于c++ - GMock StrictMock无趣的函数调用不会通过测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62601755/

10-13 09:05