I want to test whether a Qt Class correctly emits a signal upon function call using QSignalSpy
. I use MS Visual Studio and use the Microsoft::VisualStudio::CppUnitTestFramework
. Executing unit tests generally works fine, but instantiating a QSignalSpy
yields a quite generic Failed to set up the execution context to run the test
error which prevents all unit tests from executing. Compilation works fine, though.
This is the (simplified) class I want to test:
// simcam.h
#include <QtCore>
class SimCam {
Q_OBJECT
public:
SimCam() {};
~SimCam();
public slots:
void connect() {
emit(s_connected());
};
signals:
void s_connected();
};
and the unit test:
#include "CppUnitTest.h"
#include "simcam.h"
#include <QSignalSpy>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace devices_cameras_simcam_UnitTests {
TEST_CLASS(ConnectionTests) {
public:
TEST_METHOD(connectTest) {
auto simcam = SimCam();
// Problematic line
QSignalSpy spy(&simcam, &SimCam::s_connected);
simcam.connect();
spy.wait();
Assert::AreEqual(1, (int)spy.count());
}
};
}
I am a bit lost, how to get it to work or debug it. I didn't find any hint how to use QSignalSpy
in combination with Visual Studio.