This is an example of how to test an Angular Resource created with $resource
.
Important take aways:
- Do not mock $resource. Instead, you can use the resource's methods and assert that it is making the right API call. By doing that you can later change the implementation (for example for replace
$resource
for Angular Cached Resource) without needing toi go change all previous tests. - When intercepting the response of a custom method, do not transform the response string into json yourself. When you use the
transformResponse
action property of a$resource
, you replace the default angular parser that is transforming backend response from string to JSON. It has some logic to ensure that the response is a JSON and can be parsed securely. You can use it by injecting$http
into your factory and using$http.defaults.transformResponse
.