Well, as you set up the test / layout, you can fake a mail call return and verify that you get the expected result. By doing so, you will verify that the laughed answer will be correctly converted to your map operator. With your spy, you can also check how the post method was called. This will check if the parameters match what you expect.
But, in my opinion, this is a rather complicated decision. I would prefer to avoid bullying and spyware by splitting the method so that each method just does one thing. Since your addSession method currently does three different (but logically dependent) things:
- create parameters to call addSession xhr
- makes a call
- convert answer
If you break the method down to three, you can easily test method # 1 and # 3 in separate tests, and method # 2 will only contain the http library call. This allows you to achieve the same test value as above without calling the http library.
Now what about method number 2 ... it is still untested and, in my opinion, there is no reason to test it at all. Because you are not writing this code. Also, if you use the HTTP corner module, I am sure that they have robust unit tests.
Your service response should already be covered by an additional integration test, less often checking that the api service will still return what you expect.
If you really want one line to be green in your code coverage, then you could use a library called nock. Nock will intercept all xhr traffic generated by your application. In the test file, you can match xhr requests to make fun of the answers with a nock object.
var scope = nock('http://myapp.iriscouch.com') .post('/users', { username: 'pgte', email: ' pedro.teixeira@gmail.com ' }) .reply(201, { ok: true, id: '123ABC', rev: '946B7D1C' });
copied from: https://www.npmjs.com/package/nock
For help and additional information on testing in general and how much you need to check, I recommend watching Justin Searle's "Budget Reputation"
source share