Interesting timing :) If I'm understanding correctly, then I just
started work on a small helper class + macros to help with this
situation.
It's too early to make any pronouncements, but my prototype test case
I'm working on currently works so far, although I haven't tried
anything complex, yet, so everything may well come to naught ;)
The class I'm testing is called simply "Downloader", and I'm EXPECTing
it to emit its "completed(const QByteArray& downloadedData)" with the
correct downloadedData.
To declare the mock:
class DownloaderSignalSpy : public QSignalMock
{
public:
MOCK_METHOD1(completed, void(const QByteArray&)); // i.e.,
this ill be called every time the spied on object
// emits the
corresponding signal, completed(const QByteArray&).
REGISTER_FOR_SIGNALS(DownloaderSignalSpy)
{
SIGNAL_1(completed, (const QByteArray&));
}
};
The syntax has more duplication than I would like (I would have
preferred a single "MOCK_SLOTx" for each slot instead of
"MOCK_METHODx" followed by a further SIGNAL_x, duplicating the slot
name and arguments), but I've wracked my brains trying to think of a
solution and can't, yet (I'm beginning to think it's impossible). The
basic idea is to give DownloaderSignalSpy a QObject, q say, and then
for each signal that q can emit and that we're interested in, add a
MOCK_METHOD with the signal's name and args, then register our
interest using SIGNAL_x yadda yadda in the (mandatory)
REGISTER_FOR_SIGNALS block. All signals registered are then, when q
emits them, routed to the corresponding MOCK_METHOD using some of the
techniques described in
http://doc.qt.nokia.com/qq/qq16-dynamicqobject.html
plus a bunch of ugly macro-hackery :)
Setting expectations is as expected:
Downloader downloader;
DownloaderSignalSpy downloaderSignalSpy(&downloader);
EXPECT_CALL(downloaderSignalSpy, completed(correctDownloadedData));
<start off the downloader, etc>
Note in particular that MOC is not required to be run on
DownloaderSignalSpy!
Is this the kind of thing you were after? I was about to ask the group
if there was a way to query whether a mock's expectations are
currently satisfied *without* clearing them (which rules out the
otherwise-ideal "Mock::VerifyAndClearExpectations") so that I can
implement a "hang around for a timeout to allow any EXPECTed signals
to come in" which will make it usable - at the moment, you can only
either a) not wait for asynchronous signals, potentially leading to
false failures if the signals aren't emitted by the time the mock is
destroyed or b) *always* wait the full timeout, which is too slow :/
PS Apologies if this message came through twice.