[英]Google Test Gmock - Mocking Class Templates, trouble on EXPECT_CALL
Trying to implement Unit tests for a Class Template with Gtest and Gmock, but having some troubles with EXPECT_CALL.尝试使用 Gtest 和 Gmock 为 Class 模板实施单元测试,但在使用 EXPECT_CALL 时遇到了一些问题。
My Abstract Class:我的摘要 Class:
#pragma once
template <class T>
class AbstractMessageQueue {
public:
virtual ~AbstractMessageQueue() {}
virtual T dequeue() = 0;
}
My MockClass: mocks/MockMessageQueue.hpp
我的 MockClass:
mocks/MockMessageQueue.hpp
#include <gmock/gmock.h>
#include "AbstractMessageQueue.hpp"
template <class T>
class MockMessageQueue : public AbstractMessageQueue<T> {
public:
MockMessageQueue(){}
~MockMessageQueue(){}
MOCK_METHOD(T, dequeue, (), (override));
}
My Test:我的测试:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "./mocks/MockMessageQueue.hpp"
using ::testing::StrictMock;
namespace my {
namespace project {
namespace {
class TestFixture : public ::testing::Test {
public:
StrictMock<MockMessageQueue<int>> a{};
AbstractMessageQueue<int>& queue = a ;
};
TEST_F(TestFixture, test1){
EXPECT_CALL(queue, dequeue()).Times(1); //!!ERROR error: ‘class AbstractMessageQueue<int>’ has no member named ‘gmock_dequeue’; did you mean ‘dequeue’?
queue.dequeue();
}
}}}
I get the following compilation error on the EXPECT_CALL line:我在 EXPECT_CALL 行上收到以下编译错误:
error: 'class AbstractMessageQueue<int>' has no member named 'gmock_dequeue'; did you mean 'dequeue'?
I cannot figure out what is the problem here.我无法弄清楚这里有什么问题。 If I comment the line with
EXPECT_CALL
i can make the test compile, and it will fail because of:如果我用
EXPECT_CALL
注释该行,我可以使测试编译,它会因为以下原因而失败:
Uninteresting mock function call - returning default value.
Which in my view means the Mock is indeed working and the failure is caused by the StrickMock
在我看来,这意味着 Mock 确实在工作,而失败是由
StrickMock
引起的
Can someone shine some light?有人可以发光吗? Thanks
谢谢
EXPECT_CALL
expects the mock object. EXPECT_CALL
期望模拟 object。
EXPECT_CALL(queue, dequeue()).Times(1);
should be应该
EXPECT_CALL(a, dequeue());
.Times(1)
is odd and removed. .Times(1)
是奇数并被移除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.