I have generic service that create resources for my application:
(function(module) {
module.provider('restService', {
resourceRegistry: {},
addRestResource: function(entityName, entityProto) {
this.resourceRegistry[entityName] = entityProto;
},
$get: function() {
var restService;
for (var entityName in this.resourceRegistry) {
createRestResource(entityName, this.resourceRegistry[entityName]);
};
restService = {
//createRestResource: createRestResource
};
return restService;
}});
function createRestResource(entityName, entityProto) {
console.log('registering model: ' + entityName);
module.provider(entityName, { $get: function($resource, $http) {
var resource = $resource('/api/' + entityName + '/:id', { // TODO use config
id : '@id' //this binds the ID of the model to the URL param
},{
query : { method : 'GET', isArray : true }, //this can also be called index or all
update : { method : 'PUT' },
create : { method : 'POST' },
destroy : { method : 'DELETE' }
});
// here gose some other functionality unrelated to the topic...
return resource;
}});
}}(angular.module('restService', ['ngResource'])));
I can be used by any other module using
module.config(['restServiceProvider', function(restServiceProvider) {
restServiceProvider.adRestResource('User', { name: null, email: null });
}
And while above actually works for me in the application I cannot get working jasmine/karma test for it. The problem is that trying various method to configure restServiceProvider I always end with error stating that for eg TestEntity
there is unknown provider TestEntityProider
. Although I have tried different approaches to configure the resourceRegistry
before the resource is being created, here is some test file.
describe('testing restService', function () {
var httpBackend;
var theRestServiceProvider;
beforeEach(function() {
module('ngResource');
module('restService');
var fakeModule = angular.module('test.app.config', ['ngResource'], function () {});
fakeModule.config( function (restServiceProvider) {
theRestServiceProvider = restServiceProvider;
restServiceProvider.addRestResource('TestModel', {testProp: null});
});
module('test.app.config');
});
beforeEach(function () {
inject(function ($httpBackend) {
httpBackend = $httpBackend;
})
});
beforeEach(inject(function (restService) {}));
describe('create restService entity', function() {
it('should post entity with nonempty testProp',
function() {
theRestServiceProvider.addRestResource('TestModel', {testProp: null});
inject(function(TestModel) {
var object = new TestModel();
object.testProp = 'John Doe';
httpBackend.expectPOST(/.*/,
function(postData) {
console.log("post data: " + postData);
jsonData = JSON.parse(postData);
expect(jsonData.testProp).toBe(object.testProp);
return jsonData;
}).respond({});
var response = object.$create();
httpBackend.flush();
});
});
});
});
IMO this is because within the test the registering resource is being done 'too late' but still I don't know how to do this right.
Aucun commentaire:
Enregistrer un commentaire