Search code examples
node.jsmocha.jschaisupertestnock

Nock - Bodies don't match


Why does Nock give me an error saying that bodies don't match??

here is my code.

it('Should Delete /user/removeuserskills', function(done){

    mockRequest
    .delete('/user/removeuserskills',{skill:'accountant'})
    .reply(201,{
      'status':200,
      'message': '200: Successfully deleted skill'
      })
    .log(console.log)
    request
    .delete('/user/removeuserskills',{skill:'accountant'})
    .end(function(err, res){
      if(err){
        console.log(err);
      }
      else{
      expect(res.body.status).to.equal(200);
      expect(res.body.message).to.equal('200: Successfully deleted skill');}
      done();
    });

  });

I get this response when I use the .log

I have no clue why it tells me bodies dont match. I get this specifically.

matching http://localhost:8080 to DELETE http://localhost:8080/user/removeuserskills: true 
bodies don't match:                                                                        
 { skill: 'accountant' }                                                                   

{ Error: Nock: No match for request {                                                      
  "method": "DELETE",                                                                      
  "url": "http://localhost:8080/user/removeuserskills"                                     
}                                                                                          

Solution

  • There is an open issue at github that you currently are not able to use .delete(url, data).

    But you can fix it easily like this:

    mockRequest
    	.delete('/user/removeuserskills', {skill: 'accountant'})
    	.reply(201, {
    		'status': 200,
    		'message': '200: Successfully deleted skill'
    	})
    	.log(console.log)
    
    request
    	.delete('/user/removeuserskills')
    	//Just call .send here instead
    	.send({skill: 'accountant'})
    	.end(function (err, res) {
    		...
    		done();
    	});

    If you call .send(data) instead of passing data into .delete method it works just fine.