作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用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
(以及NiceMock
和NaggyMock
,是默认的)是这样的事实:
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/
因为我使用的是 RhinoMocks 3.6 版并且我没有使用 Record-Replay 并且我没有调用 Verify 方法来对模拟进行断言; 你能解释一下非常简单的区别吗? MockReposit
我了解 Mock 和 Stub 之间的区别。 但是RhinoMock框架中不同类型的Mock让我感到困惑。 有人可以用RhinoMock框架解释一下Mocks、StrictMocks、DynamicM
我是一名优秀的程序员,十分优秀!