Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you describe is a perfectly normal situation, and there are established patterns that can help you implement your tests in a way that won't affect your production code. </p> <p>One way to solve this is to use a <a href="http://xunitpatterns.com/Test-Specific%20Subclass.html" rel="nofollow noreferrer">Test Specific Subclass</a> where you could add a setter for the socket member and use a mock socket in the case of a test. Of-course you would need to make the variable protected rather than private but that's probably no biggie. For example:</p> <pre><code>class RPCClientProxy { ... protected: Socket* pSocket; ... }; class TestableClientProxy : public RPCClientProxy { TestableClientProxy(Socket *pSocket) { this-&gt;pSocket = pSocket; } }; void SomeTest() { MockSocket *pMockSocket = new MockSocket(); // or however you do this in your world. TestableClientProxy proxy(pMockSocket); .... assert pMockSocket-&gt;foo; } </code></pre> <p>In the end it comes down to the fact that you often (more often than not in C++) have to design your code in such a way as to make it testable and there is nothing wrong with that. If you can avoid these decisions leaking out into the public interfaces that may be better sometimes, but in other cases it can be better to choose, for example, dependency inject through constructor parameters above say, using a singleton to provide access to a specific instance.</p> <p>Side note: It's probably worth taking a look through the rest of the <a href="http://xunitpatterns.com" rel="nofollow noreferrer">xunitpatterns.com</a> site: there are a whole load of well established unit-testing patterns to understand and hopefully you can gain from the knowledge of those who have been there before you :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload