mardi 4 août 2015

Unit test nodeJS RESTful api

As its begining of my journey with unit test I have specific question about 'should that be unit tested or not'. Ive read few articles but not everything is clear for me - I guess particular decision need practice.

Situation more or less sees like this:

I have NodeJS service appplication with RESTful api. As you can guess it allows us to consume endpoints with proper headers/body/parameters.

Headers are kind of complicated - I mean, it contains authorization field and couple more. Ive figure out that I have to make a few 'worker' oraz 'factories' however we call it to provide those things.

So pseudo-code would looks like this:

DataCreator.js - is responsible for providing the whole object passed to endpoint(example below)

function PostData(schema) {

return {
    data: CreatorWorker.dataForPost(someParamsToEvaluate),
    headers: Headers.PostHeader(),
    timeout: Config.GetTimeoutInMs();
 };
}

We could use this one as:

SomeFileWithServer.js (UrlCreator is another piece of program responsible for.. of course providing proper URL)

rest.post(UrlCreator.SetPostUrl(), DataCreator.PostData())

My question is : how would unit test of DataCreator look like? What do we test here? Presence of data, headers, timeout fields? We should mock/stub all used methods (dataForPost, PostHeader...) and after that check if provided values are assign to proper fields? - or maybe both.

Going deeper.. for instance CreatorWorker.dataForPost(someParamsToEvaluate) providing body for reuqest, so inside we just check if its created in good way with proper 'someParamsToEvaluate'. --> So this situation is clear for me, one method.. it does a thing so we should check it if its done right.

So the question is, should we in such situations link particular method (e.g Headers.PostHeader) to field (headers) ? Its a good practice? Maybe we should just check if its get called? Or as I mention before we should stub it and check if headers value equals that (thats the 'linking').

Ive imagined situation when we want to refractor names of those methods, then we will have to maintain those tests.. There's plenty situations like that so I'd like to know good pattern for that.

I hope its explained clearly!

Aucun commentaire:

Enregistrer un commentaire