I have the following service (reduced for simplicity):
angular.module('myApp')
.factory('BasicTimerService', function BasicTimerService(PromiseService)
{
var isPlaying = false,
maxEventTime = 0;
return {
start: function()
{
// only "start" once this promise has resolved.
PromiseService.get_promise()
.then(function(value)
{
maxEventTime = PromiseService.maxEventTime();
isPlaying = true;
});
},
get_isPlaying: function() {
return isPlaying;
}
};
});
I wish to test that maxEventTime is only set after the Promise is successful.
My test:
describe('Service: BasicTimerService', function () {
beforeEach(module('myApp'));
var mockPromiseService,
BasicTimerService,
$q,
$rootScope,
deferred;
beforeEach(function(){
mockPromiseService = {
maxEventTime: function() {
return 17000;
}
};
module(function($provide){
$provide.value('PromiseService', mockPromiseService);
});
inject(function(_BasicTimerService_, _$q_, _$rootScope_){
BasicTimerService = _BasicTimerService_;
$rootScope = _$rootScope_;
$q = _$q_;
});
mockPromiseService.get_promise = function() {
var deferred = $q.defer();
deferred.resolve('hello world');
return deferred.promise;
};
});
it('should get maxEventTime once the promise is resolved.', function() {
spyOn(mockPromiseService, 'get_promise').and.callThrough();
spyOn(mockPromiseService, 'maxEventTime').and.callThrough();
$rootScope.$apply();
BasicTimerService.start();
expect(mockPromiseService.get_promise).toHaveBeenCalled();
expect(BasicTimerService.get_isPlaying()).toBe(true);
expect(mockPromiseService.maxEventTime).toHaveBeenCalled();
});
});
I've determined that mockPromiseService.get_promise() is successfully called (the first expect() is successful), but the .then() clause in BasicTimerService does not seem to be called, and I cannot figure out why.
I have tried using both $rootScope.$apply();
as well as $rootScope.$digest();
to no avail.
Any ideas as to why this does not work?
Aucun commentaire:
Enregistrer un commentaire