title: Qt event loop and unit testing?
body: <p>I'we started experimenting with unit testing in Qt and would like to hear comments on a scenario that involves unit testing signals and slots.</p>
  
  <p>Here is an example:</p>
  
  <p>The code i would like to test is (m_socket is a pointer to <code>QTcpSocket</code>):</p>
  
  <pre><code>void CommunicationProtocol::connectToCamera()
  {
      m_socket-&gt;connectToHost(m_cameraIp,m_port);
  }
  </code></pre>
  
  <p>Since that is an asynchronous call i can't test a returned value. I would however like to test if the response signal that the socket emits on a successful connection (<code>void connected ()</code>) is in fact emitted.</p>
  
  <p>I've written the test below:</p>
  
  <pre><code>void CommunicationProtocolTest::testConnectToCammera()
  {
      QSignalSpy spy(communicationProtocol-&gt;m_socket, SIGNAL(connected()));
      communicationProtocol-&gt;connectToCamera();
      QTest::qWait(250);
      QCOMPARE(spy.count(), 1);
  }
  </code></pre>
  
  <p>My motivation was, if the response doesn't happen in 250ms, something is wrong.</p>
  
  <p>However, the signal is never caught, and I can't say for sure if it's even emitted. But I've noticed that I'm not starting the event loop anywhere in the test project. In the development project, the event loop is started in main with <code>QCoreApplication::exec()</code>.</p>
  
  <hr>
  
  <p>To sum it up, when unit testing a class that depends on signals and slots, where should the </p>
  
  <pre><code>QCoreApplication a(argc, argv);
  return a.exec();
  </code></pre>
  
  <p>be run in the test environment?</p>