angularjs - personalizadas - Prueba de un controlador que ejecuta un servicio con $ recurso de respuesta
directivas personalizadas angular 5 (1)
simula el método de servicio y devuelve una promesa, luego resuélvela cuando la necesites.
describe(''Test Controller'',function(){
var postDefer;
beforeEach(module(''myModule''));
beforeEach(inject(function($controller, MyService, $q){
postDefer = $.defer();
spyOn(MyService, ''post'').and.returnValue(postDefer.promise);
this.ctrl = $controller(''myCtrl'', {MyService : MyService});
}))
it(''after save value is 1'',function(){
this.ctrl.post({name: ''chuck''});
postDefer.resolve();
scope.$apply();
expect(this.ctrl.value).toBeEqual(1);
});
});
Tengo un controlador que usa un servicio, resuelve una promesa y luego establece una variable. Quiero probar si la variable se ha establecido, ¿Cómo hago esto?
Service.js
angular
.module(''myModule'')
.service(''MyService'',[''$resource'',function($resource){
var r = $resource(''/users/:id'',{id:''@id''});
this.post = function(_user){
return (new r(_user)).$save()
}
}])
Controlador
angular
.module(''myModule'')
.controller(''myCtrl'', [''MyService'',function(MyService){
var vm = this;
vm.value = 0;
vm.post = function(_user){
MyService.post(_user).then(function(){
vm.value = 1;
});
};
}])
controller_spec
describe(''Test Controller'',function(){
beforeEach(module(''myModule''));
beforeEach(inject(function($controller, MyService){
this.ctrl = $controller(''myCtrl'');
}))
it(''after save value is 1'',function(){
this.ctrl.post({name: ''chuck''});
//Always this.ctrl.value it''s equal to 0
expect(this.ctrl.value).toBeEqual(1);
});
});