0

I was wondering if anyone could give me some pointers on designing my unit tests, specifically where the function being tested appears in the test itself other than in the unit's action.

I'm writing server side javascript with node and using rsvp for asynchronous calls to a restful backend.

I structure my unit tests as follows.

//Setup
//Action
//Assertion
//Cleanup

Right now, I'm building a function which asynchronously deletes enteries in a database so my unit test looks like this.

"deletingFunction_1entry_returns1EntryDeleted": function() {
      return new rsvp.Promise(function( resolve, reject ) {
      //Setup
      var DatabaseEntry = { "Info": "Info" };
      setup( DatabaseEntry ).then(function( xPostedEntries ) {
      //if the setup promise resolves then...
          //Action
          xDelete( xPostedEntries ).then(function( xDeletedEnteries ) {
              //Assertion
              if ( xPostedEntries === xDeletedEntries ) {
              //if the posted enteries matched the deleted enteries then...
                  resolve();
              } else {
              //otherwise the posted enteries did not match the deleted  
              //enteries so...
                  reject("The posted enteries did not match the deleted enteries.");
              }    
          });
      }, function( xPostedEnteries ) {
      //otherwise the setup promise did not resolve so...
          //Cleanup
          /***************************************************************/
          /* Here's where I'm having trouble. If the setup fails to post */
          /* all of the entries to the database then the test is a       */                              
          /* scratch but I want to clean up all the posts that 'were'    */
          /* posted but to remove them from the database, I'd be using a */
          /* deleting function, which is the function that I'm testing.  */
          /* So how can I redesign my unit test not to use the function  */
          /* that it's testing.                                          */
          /***************************************************************/
          reject("The setup failed.");   
      });
});

}

Edited: I wrote a function which truncates the database table for clean up but would still appreciate pointers on how to improve my testing.

ogginger
  • 433
  • 4
  • 11

1 Answers1

0

This isn't a unit test because it has side-effects. Of course you can't use the "delete" function that you're testing. It doesn't even exist until it passes its unit tests. Your unit test needs to be a unit test.

Probably, you want the database connection to be dependency injected, as an abstract datastore. The unit test will use a mock datastore.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Thank you for the response. What do you mean by "side-effects"? Do you have any constructive comments to add? I wrote another function that truncates the database table for clean up as quick fix but I'd still appreciate tips on redesigning or cleaning up the test so that it's legit. – ogginger Jan 15 '16 at 22:17
  • @user2480206 don't talk to the database in a unit test at all. https://en.wikipedia.org/wiki/Side_effect_(computer_science) – djechlin Jan 15 '16 at 22:24
  • Is there a type of test that would be better suited for this type of method? Or I'm conceptually having difficult understanding how you would test a method that did work on a database without talking to the database. – ogginger Jan 15 '16 at 22:45
  • 1
    @user2480206 you probably want a unit test. Pass the datastore to the class/function via dependency injection and use a mock datastore in the unit test. – djechlin Jan 15 '16 at 22:48
  • Ahhh Ok. Got it. Thank you very much! – ogginger Jan 15 '16 at 22:49