Reputation: 6459
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
.
Upvotes: 4
Views: 1668
Reputation: 1612
Let me see if I can explain what Sulthan meant.
Use httpBackend to write unit tests that test the REST API calls in order to mock the server API. In this case, you want to test that API makes the right calls with the right arguments and receives the right response without actually making an http request, so the server is the black box.
Use spy to test code that uses the client side API code. For example, a controller would use the client side Restangular service that calls the API. In this case, the black box is Restangular and only care about the expected response.
Upvotes: 3
Reputation: 130152
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.
Upvotes: 6