如何解决在使用 googletest 进行模拟时,如何为重载方法指定 internal::AnythingMatcher 的参数类型? myInterfaceToMock.hmyMock.hmain.cpp
初始问题
在使用 googletest 进行测试时,我使用了一个模拟类,其中包含一个重载方法,其中一个参数的类型不同。
EXPECT_CALL(mockedClass,mockedMethod(_,_)).WillOnce(Return(true));
我尝试将 internal::AnythingMatcher
下划线与 https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#SelectOverload 中所示的类型规范结合起来
用 An<type>()
、TypedEq<type>(_)
或 Matcher<type>(_)
替换有问题的参数,并编写如下内容:
Matcher<type> customAnythingMatcher = _;
EXPECT_CALL(mockedClass,customAnythingMatcher)).WillOnce(Return(true));
更新 #1
根据要求,我通过提供一个最小的工作——或者在这种情况下是一个非工作——来详细说明我想要实现的目标:
myInterfacetoMock.h
#ifndef MYINTERFACetoMOCK_H
#define MYINTERFACetoMOCK_H
#include <QVariant>
#include <QtGlobal>
class MyInterfacetoMock {
virtual bool myMethod(QVariant &firstArgument,const quint8 secondArgument) const = 0;
virtual bool myMethod(QByteArray &firstArgument,const quint16 secondArgument) const = 0;
virtual bool myOtherMethod(quint8 firstAndOnlyArgument) const = 0;
};
#endif // MYINTERFACetoMOCK_H
myMock.h
#ifndef MYMOCK_H
#define MYMOCK_H
#include <gmock/gmock.h>
#include <QtCore/QObject>
#include "myInterfacetoMock.h"
class MyMock : public MyInterfacetoMock {
public:
// virtual bool myMethod(QVariant &firstArgument,const quint8 secondArgument)
// const = 0;
MOCK_METHOD(bool,myMethod,(QVariant &,const quint8),(const,override));
// virtual bool myMethod(QByteArray &firstArgument,const quint16
// secondArgument) const = 0;
MOCK_METHOD(bool,(QByteArray &,const quint16),override));
// virtual bool myOtherMethod(quint8 firstAndOnlyArgument) const = 0;
MOCK_METHOD(bool,myOtherMethod,(quint8),override));
};
#endif // MYMOCK_H
main.cpp
#include <gmock/gmock-matchers.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <QCoreApplication>
#include <QTimer>
class Tester : public QObject {
Q_OBJECT
public:
Tester(QObject *parent = nullptr) : QObject(parent) {}
public slots:
void run() {
::testing::InitGoogletest();
emit(result(RUN_ALL_TESTS()));
}
signals:
void result(int res);
};
#include "main.moc"
int main(int argc,char *argv[]) {
QCoreApplication app(argc,argv);
::testing::InitGoogleMock(&argc,argv);
Tester *tester = new Tester(&app);
QObject::connect(tester,&Tester::result,&app,&QCoreApplication::exit);
QTimer::singleShot(0,tester,SLOT(run()));
return app.exec();
}
#include "myMock.h"
TEST(myTest,testMockOverload) {
using namespace testing;
MyMock mock;
// Reference method,which does work all the time
EXPECT_CALL(mock,myOtherMethod(_)).WillRepeatedly(Return(false));
// Using a defined value as argument
QVariant myVariant;
EXPECT_CALL(mock,myMethod(myVariant,3)).WillOnce(Return(true));
bool result = mock.myMethod(myVariant,3);
ASSERT_TRUE(result);
// Anything matcher for the unambiguous argument
EXPECT_CALL(mock,_)).WillOnce(Return(true));
result = mock.myMethod(myVariant,3);
ASSERT_TRUE(result);
// Trying to match any passed qvariant
EXPECT_CALL(mock,myMethod(An<QVariant>(),_)).WillOnce(Return(true));
}
最后一行出现编译错误:
..\googlemock_disambiguate\main.cpp(55): error C2664: 'testing::internal::MockSpec<bool (QByteArray &,quint16)> MyMock::gmock_myMethod(const testing::internal::WithoutMatchers &,const testing::internal::Function<bool (QByteArray &,quint16)> *) const': cannot convert argument 1 from 'testing::Matcher<QVariant>' to 'const testing::Matcher<QVariant &> &'
..\googlemock_disambiguate\main.cpp(55): note: Reason: cannot convert from 'testing::Matcher<QVariant>' to 'const testing::Matcher<QVariant &>'
..\googlemock_disambiguate\main.cpp(55): note: No user-defined-conversion operator available that can perform this conversion,or the operator cannot be called
我猜它可能有什么东西。与方法的常量性有关,但我仍然不明白该错误的含义...
解决方法
使用 An<type>
是正确的解决方案。
EXPECT_CALL(mockedClass,mockedMethod(_,An<type>())).WillOnce(Return(true));
如果它对您不起作用,则问题出在其他地方。也许你可以详细说明?
,自己回答问题:
我只是没有正确指定类型,因为我希望通过引用。我总是尝试使用诸如 An<type>()
之类的东西,而我应该使用 An<type&>()
代替。
所以,根据我上面的例子,正确的行是:
EXPECT_CALL(mock,myMethod(An<QVariant&>(),_)).WillOnce(Return(true));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。