[Interest] Mocking Network calls in code using QNetworkAccessManager

Konstantin Tokarev annulen at yandex.ru
Thu May 18 12:42:00 CEST 2017

18.05.2017, 13:35, "Shantanu Tushar" <shaan7in at gmail.com>:
> Hi,
> In one of the unit tests that I am trying to write, I call a function
> that makes a get() call using a QNetworkAccessManager that I pass to
> it. It looks something like this-
> bool trackEvent(QString event, QNetworkAccessManager *nam);
> However, I do not want actual HTTP calls being made during the unit
> test, so I thought I'll write a FakeNetworkAccessManager and pass that
> (where I write a mock get()). The issue is that the get() on
> QNetworkAccessManager is not virtual, so at the end my mock get() is
> useless.

You need to override createRequest()
You may also need to add mock subclass for QNetworkReply.

> Any recommendations on how to do I achieve the unit test without
> needing network access?
> Cheers,
> --
> Shantanu Tushar (UTC +0530)
> shantanu.io
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest


More information about the Interest mailing list