QTest a connection from a private member
-
Thank you for your answer @Axel-Spoerl !
Could you please elaborate your answer?
I'm very sorry but I don´t understand what you mean...Are you talking about modifying the code to test?
I would like not to have to modify the code to test. Although a minor modification might be possible... -
Can you show the declaration of the method you want to test?
Probably it’s in the
private
Section of the header. A connection won’t work here. You have to put it in a
private slots
section.
-
It needs to be a slot, no matter if private or public.
-
ok, let me see if I understand well.
so the following code does not work because the object "aPrivateMember" is actually private. if it was public the code actually works.the test code
void MainWindowTest::init ( ){} void MainWindowTest::cleanup ( ){} void MainWindowTest::test_case ( ) { qwidget_instance = new QWidget( ); mainWindow_instance = new myMainWindow( qwidget_instance ); mainWindow_instance->aFunctionToTest( ); QCOMPARE( (bool) QObject::connect(mainWindow_instance->aPrivateMember, &aClass::aSignal, mainWindow_instance, &myMainWindow::aSlot, Qt::UniqueConnection), false ); // the test passes if, and only if aPrivateMember was declared in the public section. // if aPrivateMember is private the following compile error occurs: // error: 'aClass* myMainWindow::aPrivateMember' is private within this context } QTEST_MAIN( MainWindowTest )
the code to test
.h class myMainWindow: public QMainWindow { Q_OBJECT public slots: void aFunctionToTest ( ); void aSlot ( ); private: aClass aPrivateMember; }; .cpp void myMainWindow::aFunctionToTest( ) { connect( aPrivateMember, &aClass::aSignal, this, &myMainWindow::aSlot); }
So my question stays the same. Is it possible to test the success of the connection done in aFunctionToTest given that aPrivateMember is actually private?
Thank you so much for the patience!
I tried to be as clear as possible now.And I'm pretty sure I must be missing something obvious.. but I cant see what it is...
-
Ah, now I understand better.
The private member can’t be tested (called) in an external test class normally. It also can’t be invoked / connected, because it isn’t a slot.
So if you want to connect it, it has to be a slot. If you want to keep it private, you could befriend the test class. -
@wasawi2 @Axel-Spoerl Actually with the Qt5 connect syntax you can connect methods and lambdas which are not declared as slots. So, it should be possible without code modification. You just need to make the test class friend, so it can access non-public members of the class being tested.
-
@jsulm said in QTest a connection from a private member:
You just need to make the test class friend
So as the OP is asking this does require the code being tested to be changed!
@wasawi2
Whichever way up you look at it, you will have to make a change to allow aprivate
member to be accessed outside. Which is whatprivate
means (i.e. cannot access outside, unless you change that or put infriend
) :) -
@JonB said in QTest a connection from a private member:
So as the OP is asking this does require the code being tested to be changed!
Well, yes. But it does not require to change the visibility of the class members/methods.
-
@jsulm
I absolutely concur, hence my:[private] cannot access outside, unless you change that or put in
friend
The point being to answer OP's
So If i understand correctly without modifying the code to test there is no way to test the connection.
They will have to make some change, which doubtless they were attempting to avoid. Just a heads-up.
-