Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use spy vs $httpBackend in jasmine

I'm writing Jasmine code to test some Restangular logic. I want to test that my 'Foo' object has been Restangulraized, so that the foo.getList() method will call GET /foo and return the result

I could test this in two ways. I could add a .spy on foo.getList() and have it return the expected results. Alternatively I can use $HttpBackend.whenGET("/foo") and set my expected results there.

Is one of these considered preferable?

I would assume HTTPBackend would be the better option, since it test 'later' in the logic flow. If I used a spy I couldn't prove, for example, that my Restangularize hadn't screwed up and was trying to resolve a different URL.

However, I'm looking at the inherited tests and they all use spy, and since I assume the person who wrote this code is better than me (they can't be more newbie then me at Angular) it makes me wonder if there is an advantage to the use of spy over $httpBackend.

like image 634
dsollen Avatar asked Jan 09 '23 17:01

dsollen


1 Answers

When you are testing foo.getList(), use httpBackend. Once you test functions that only use foo.getList(), then use a spy. That's the most simple solution that avoids test duplication.

Note that you are writing unit tests. Every unit should be independent on other units.

like image 93
Sulthan Avatar answered Jan 11 '23 23:01

Sulthan