I have some class which can emit some signal with another my class. Example:
class CMyClass : public QBytaArray
{
void SomeAction();
}
class CMainClass : public QObject
{
signals:
void testSignal(const CMyClass &myClass);
public:
void test() {
CMyClass data;
emit testSignal(data);
}
}
I wrote unix test for CMainClass:
header:
#ifndef TESTMAINCLASS_H
#define TESTMAINCLASS_H
#include <QObject>
#include <QSignalSpy>
#include "MainClass.h"
Q_DECLARE_METATYPE(CMyClass)
class CTestMainClass : public QObject
{
Q_OBJECT
public:
explicit CTestMainClass(QObject *parent = 0);
private slots:
void testS();
};
#endif // TESTMAINCLASS_H
Source:
#include <QDebug>
#include "TestMainClass.h"
CTestMainClass::CTestMainClass(QObject *parent) : QObject(parent)
{
qRegisterMetaType<CMyClass>();
}
void CTestMainClass::testS()
{
qDebug() << "Test signal emitor";
CMainClass prc;
QSignalSpy spy(&prc, SIGNAL(testSignal(const CMyClass&)));
prc.test();
CMyClass buf = qvariant_cast<CMyClass>(spy.at(0).at(0));
}
But, in log file I've got:
PASS : CTestCommandProcessor::initTestCase() QDEBUG : CTestCommandProcessor::testS() Test signal emitor QDEBUG : CTestCommandProcessor::testS() Count: 1 QDEBUG : CTestCommandProcessor::testS() Vals: QVariant(CMyClass, ) A crash occurred in C:\UnitTest\build-UnitTest-Qt_5_7_1_5_7_1_Static-Release\release\UnitTest.exe. Function time: 0ms Total time: 0ms
I've tried to change string:
CMyClass buf = qvariant_cast< CMyClass >(spy.at(0).at(0));
to
CMyClass buf = qvariant_cast< const CMyClass& >(spy.at(0).at(0));
But, I was getting the same error. What is wrong? Can I use in Qt's unittest signals with my own classes?
I'm so sory, this is my bad. I used takeFirst() method of spy. And after that spy's object doesn't have signals anymore.