Your problem is more of a design problem.
If you ever had to perform different behavior for Socket , you were fried since it involves rewriting all the code that created the sockets.
The usual idea is to use the abstract base class (interface) of the Socket , and then use the abstract Factory to create the desired socket, depending on the circumstances. Factory itself can either be Singleton (although I prefer Monoid), or passed as arguments (according to the Injection Dependency tenants). Note that the latter means that there is no global variable, which, of course, is much better for testing.
So I would advise something like:
int main(int argc, char* argv[]) { SocketsFactoryMock sf; std::string host, port;
This affects the client: you no longer hide the fact that you are using sockets behind the scenes. On the other hand, it gives control to the client, who may wish to develop some user sockets (for registration, triggering actions, etc.).
So this is a design change, but it is not caused by TDD itself. TDD simply takes advantage of a higher degree of control.
Also note the explicit ownership of the resource expressed by using unique_ptr .
source share