lundi 28 mars 2016

Unit tests: when to stub

I am wondering if there is a criterion that I can rely on to decide whether I should use real code in a given unit test or to use stubs. Here is a language independent example where I need to decide :

Response sendRequest(postdata, headers) {
  response = call(postdata, headers, 'post')
  .
  .
  .
  return parsedResponse
}

the function call is inluded from another file and reads a config file to get the url and port of the external api. I have a local light implementation of the external api just for tests (I do not need to hit the real external api).

In this example I think using the local light api will help me test better how every type of response is handled in the code to test. So do you think that it is better, in terms of unit testing best practices, to use the real "call" function and hit the local light api or just stub it and make a different stub for each kind of response?

Thank you

Aucun commentaire:

Enregistrer un commentaire