Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://en.wikipedia.org/wiki/Unit_testing#Separation_of_interface_from_implementation" rel="nofollow">to allow your tests/code to scale neatly you should decouple your code using an interface</a>, this makes updating tests after refactoring much simpler, <a href="http://en.wikipedia.org/wiki/Technical_debt" rel="nofollow">otherwise by coupling your code you incur technical debt</a> you could potentially add</p> <ol> <li>interface to decouple the code</li> <li>a mock for DoSpecialStuff to test that the decoupled code behaves as you expect</li> </ol> <p>if you where writing the code in C++ it would look something like</p> <p>Notifiers Code</p> <pre><code>Notifier.h class Notifier : public I_Notifier { void _sendNotification( NotificationInstance * notification ); }; Notifier.cpp void Notifier::_sendNotification( NotificationInstance * notification ) { //do some stuff that needs testing } </code></pre> <p>Interface to the Notifier, this is what gets added to the DoSpecialStuff class to decouple the code I_Notifier.h</p> <pre><code>class I_Notifier { void sendNotification( NotificationInstance * notification ) { _sendNotification(notification); } virtual void _sendNotification( NotificationInstance * notification )=0; } </code></pre> <p>Mock used for testing aspects of DoSpecialStuff, our mock assumes that the notifiers code works our <strong><em>unit tests for DoSpecialStuff just want to make sure sendNotification got called</em></strong>, so our test can just check the state of send_notification_called to see if it was successful or not. </p> <pre><code>Mock_Notifier.h struct Test_Notifier : public I_Notifier { Test_Notifier() : send_notification_called(false) virtual void _sendNotification( NotificationInstance * instance ) { send_notification_called = true; } bool send_notification_called; }; </code></pre> <p>DoSpecialStuff code <strong><em>Note that this class now has an interface to the notifier class</em></strong> which is what decouples the code (so our test no longer needs to include the actual class unless we are using calls from it</p> <pre><code>DoSpecialStuff.cpp class DoSpecialStuff { DoSpecialStuff( I_Notifier * n ) : notifier_(n) {} void DoSpecialStuff::_myMethod( NotificationInstance * notification ) I_Notifier * notifier_; } void DoSpecialStuff::_myMethod( NotificationInstance * notification ) { //do some stuff that needs testing n.send_notification (notification ) } </code></pre> <p>Interface</p> <pre><code>I_DoSpecialStuff.h class I_DoSpecialStuff { void myMethod( NotificationInstance * notification ) { _myMethod(notification); } virtual void _myMethod( NotificationInstance * notification )=0; } </code></pre> <p>Where the functionality is </p> <ul> <li>contact messaging server and deliver message</li> </ul> <p>on top of your unit tests you would also have a set of functional tests, so the unit tests would test each method of the classes (where appropriate), the functional test suite would start up an instance of your server and then the functional tests would call the functions that would contact the messaging server, then check the output (be it a database state or returned signal) </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.
    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