angularjs - Unit testing promises in js-data-angular models -
we use js-data
, js-data-angular
in our project.
i have following model:
(function () { 'use strict'; angular.module('dash.models') .factory('diagnosis', ['ds', function (ds) { function transform(resourcename, attrs, cb) { attrs.icd9codes.foreach(function (el) { delete el.add; }); cb(null, attrs); } this.transform = transform; return ds.defineresource({ name: 'diagnosis', idattribute: 'id', endpoint: '/diagnosis', baseurl: '/api', beforecreate: transform, beforeupdate: transform }); }]); }());
and following call said model:
var startediting = self.startediting = function(parentscope, diagnosis) { diagnosis.findall({ deep:true }, { endpoint: '/diagnosis/' + diagnosis.id }).then(function(d) { $scope.diagnosis = d; $scope.inscope = true; }); };
in unit test, mock call this:
var diagdeferred = _$q_.defer(); diagdeferred.resolve({ 'name': 'breast', 'categories': null, 'id': '026c7cd0-14ef-4312-a8f1-2092107b0e50', 'icd9codes': [{id: '1', code: '001', description: 'icd9 code'}] }); spyon(diagnosis, 'findall').and.returnvalue(diagdeferred.promise);
and actual call mocked, doesn't executed (and can't find reliable information on how done) function inside .then
of diagnosis.findall
i know code works, need cover unit tests , i'm coming dry.
thanks.
i think forgot call $scope.digest()
in test. here working fiddle.
after call startediting()
, should call $scope.$digest()
mock promise
executed , can data in then
block. hope helps.
Comments
Post a Comment