user3364192 user3364192 - 9 months ago 64
AngularJS Question

Get rid of fields added by angular $resource with $promise

If you use resource together with promise:

$resource('api/items').get(items).$promise.then((items) => {
$scope.items = items

Turns out it wraps
object and adds some additional fields. This does not affect the app itself, but fucks up the tests:

httpBackend.expectGET('api/items').respond(200, items);

which results in error:

Expected [ Resource({ items: [...], $promise: Promise({ $$state: Object({ status: 1, pending: undefined, value: <circular reference: Object>, processScheduled: false }) }), $resolved: true }) ] to equal [ Object({ items: [...] }) ].

Why angular adds this fields and how can I get rid of this $promise,$resolved etc. junk to make my tests clean?

Answer Source

Checkout the Returns section for the resource docs here

Look for the toJson function, that should help you out as it strips off the angular fields for you

A simpler way is that as you get an angular resource object back you could just assign scope.items whatever the property is on the resource that holds the data. E.g:

$scope.items = resource.items;

This will pass the test for you and is what I typically do when using ngresource as this is all that get used by my view controllers