angularjs - Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL -
i know error appears in questions lot despite efforts didn't manage solve problem.
i have service fetches categories parse. done when running test getting error shown in title.
my code following:
parseservice.js
angular.module('starter').service('parseservice', ['$q', function ($q) { this.getallcategories = function() { var deferred = $q.defer(); var categories = []; var query = new parse.query("category"); query.find({ success: function(results) { console.log('succesfully queried parse.'); (var = 0; < results.length; i++) { var result = results[i]; categories.push(result.get("name")); } deferred.resolve(categories); console.log(categories); }, error: function(error) { console.log('error querying parse.'); console.log(error); deferred.reject(error); } }); return deferred.promise; }; }]);
parseservicetest.js
describe('parseservice', function(){ var service; beforeeach(function(){ module('starter'); }); ('should fetch categories', function(done){ beforeeach(inject(function(parseservice){ service=parseservice; })); var promise = service.getallcategories(); promise.then(function(categories){ expect(categories.length).toequal(5); done(); }); }); });
now console output following:
'succesfully queried parse.' ['pets', 'automobile', 'social', 'retail', 'technology'] error: timeout - async callback not invoked within timeout specified jasmine.default_timeout_interval.
it seems service returns categories done() method never executed reason.
you can ignore fact don't mock call parse. aware not best practice.
for resolving/rejecting $q promises in tests, need start digest cycle.
describe('parseservice', function() { var service; var $rootscope; beforeeach(module('starter')); beforeeach(inject(function(parseservice, _$rootscope_) { service = parseservice; $rootscope = _$rootscope_; })); it('should fetch categories', function(done) { var promise = service.getallcategories(); promise.then(function(categories) { expect(categories.length).toequal(5); done(); }); // run digest cycle resolve $q promises $rootscope.$digest(); }); });
Comments
Post a Comment