I am trying to do unit tests of my application in AngularJS. I am creating a factory
of the inheritance of other services to communicate with the database making use of $resource
and $cacheFactory
to reduce the number of calls to the server.
The factory
in question configures an interceptor for certain calls to $resource
so that the cache is invalidated when making these calls. This is the code:
ModelFactory.$inject = ['$resource', '$cacheFactory'];
function ModelFactory($resource, $cacheFactory){
return function (baseUrl, paramDefaults, actions, options) {
var cache = $cacheFactory(baseUrl);
var params = angular.extend({id: '@id'}, paramDefaults);
var interceptor = { response: invalidateCache };
actions = angular.extend({}, actions, {
'get': { method: 'GET' },
'query': { method: 'GET', cache: cache, isArray: true },
'save': { method: 'POST', interceptor: interceptor },
'update': { method: 'PUT', interceptor: interceptor},
'remove': { method: 'DELETE', interceptor: interceptor },
'delete': { method: 'DELETE', interceptor: interceptor }
});
var model = $resource(baseUrl + '/:id', params, actions, options);
model.invalidateCache = invalidateCache;
/**
* Invalida la cache eliminando todos los registros
*/
function invalidateCache() {
cache.removeAll();
}
return model;
};
}
When testing using karma
and jasmine
, I create a spy for the function model.invalidateCache()
to verify that it is called when it corresponds. This is the code of spec
:
describe("Cached resource factory", function () {
var server;
var factory;
beforeEach(module('intranet'));
beforeEach(inject(function ($httpBackend, Model) {
server = $httpBackend;
factory = Model;
hlpHttpBackend.setCommonIntranetCalls($httpBackend)
}));
// ...
it("should invalidate the cache with every successfull call to create, update or delete action", function () {
var itemInfo = {field1: 'one', field2: 'two'};
var dbItemInfo = {id: 1, field1: 'one', field2: 'two'};
var modItemInfo = {id: 1, field1: 'one', field2: 'modified-two'};
var resource = new factory('/data');
spyOn(resource, 'invalidateCache').and.callThrough();
// Create
server.expectPOST('/data').respond(dbItemInfo);
var item = resource.save(itemInfo);
server.flush();
// Update
server.expectPUT('/data/' + item.id).respond(modItemInfo);
item.field2 = 'modified-two';
item.$update();
server.flush();
// DELETE
server.expectDELETE('/data/' + item.id).respond(null);
item.$delete();
server.flush();
// cachedResource debe haber invalidado la cache 3 veces, una por cada llamada
// a create, update y delete
expect(resource.invalidateCache).toHaveBeenCalledTimes(3);
});
});
The code of factory
works correctly, that is, calls to the interceptor are being made, but the test fails:
Expected spy invalidateCache to have been called 3 times. It was called 0 times.
Any ideas? Maybe I should not be testing this, after all, it's the code of $resource
and not mine that is responsible for calling the interceptor.