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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With