Sunday, 15 August 2010

module - AngularJS, Jasmine, and run() -



module - AngularJS, Jasmine, and run() -

it may question has been answered, cannot find addresses forgive me if reasking has been answered somewhere else.

according angularjs documentation:

run blocks - executed after injector created , used kickstart application. instances , constants can injected run blocks. prevent farther scheme configuration during application run time.

i'm writing unit tests , need test next code:

'use strict'; angular.module('htmlfwapp', []).run(['applicationmodel', function(applicationmodel) { applicationmodel.mergefromjsonasync('application.config.json'); // code makes phone call 'application.config.json' // , merges via _.extend applicationmodel. } ]); beforeeach(module('htmlfwapp')); beforeeach(inject(function($httpbackend) { $httpbackend.whenget('application.config.json').respond({ 'applicationnameshort': 'jasmine config', 'applicationnamelong': 'jasmine config', 'applicationdescription': 'this test of everything!', 'applicationkey': 'test' }); })); describe('applicationmodel test', function() { it('should inject , defined', inject(function(applicationmodel) { expect(applicationmodel).tobedefined(); })); it('should merge properties provided json', inject(function(applicationmodel) { expect(applicationmodel.applicationnameshort).tobe('jasmine config'); })); });

the problem run method not called before test. there way trigger run called or missing obvious?

after doing digging , asking around, found solution. set $q promise on applicationmodel , in test wait promise resolve:

it('should merge properties provided json', inject(function(applicationmodel, $httpbackend) { runs(function () {console.log('starting!')}); waitsfor(function () { homecoming applicationmodel.loadingpromise === undefined; }); runs(function () { expect(applicationmodel.applicationnameshort).tobe('jasmine config'); }); $httpbackend.flush(); }));

angularjs module jasmine

No comments:

Post a Comment