mercredi 4 mars 2015

Jasmine mock promise resolve not being handled when called in controller construtor

In my controller constructor, a lot of the private variables are set as the result of a promise returned from a service.


For example, this will be called when the controller is being constructed.



MyService
.initializeDataForType(type)
.then(function (data) {
//never getting hit
});


And the service call is mocked to return a resolved promise like so.



var myService = jasmine.createSpyObj('page.MyService', [
'initializeDataForType'
]);

beforeEach(inject(function ($controller, _$q_) {
myService.initializeDataForType.and.callFake(function (type) {
var deferred = _$q_.defer();
deferred.resolve({});
return deferred.promise;
});

target = $controller('page.MyController', {
'page.MyService': MyService
});
}));


The then() method for the service call is never being reached. It seems that jasmine isn't waiting and moving on to the next test.


Aucun commentaire:

Enregistrer un commentaire