mercredi 10 avril 2019

How to avoid calling a method in unit tests?

I'm writing tests for a class A. The behavior this class is supposed to implement is starting a connection to a backend system. It offers the public function StartConnection() via its interface.

void A::StartConnection(const std::string uri) {
  // various method calls...
  StartIocThread();
  // more method calls and stuff to do...
}

StartConnection includes calls to various private methods, such as a method to regex the URI or set up handler. One of these methods is called StartIocThread which launches a new thread which will later invoke the handlers.

void A::StartIocThread() {
  std::thread([&] { ioc_.run(); }).detach();
}

Now obviously I don't want a new thread to be detached every time I run the StartConnection unit test. However, this method gets called inside the StartConnection() method. What is the best way to prevent a new thread being created every time?

The only way I can think of right now is putting that method inside a different class which I can then mock in the unit test. But that seems like a lot of overhead (including dependency injection) just to prevent this method from being called.

Thanks in advance :)

Aucun commentaire:

Enregistrer un commentaire