Offering a straight forward service:
.factory('list', function($q, $timeout) {
return {
get: function() {
var dfd = $q.defer();
$timeout(function () {
dfd.resolve(['label1', 'label2']);
}, 10);
return dfd.promise;
}
};
});
I'm eager to put it to the test. That's why I crafted:
describe('list', function() {
var list, labels;
beforeEach(module('app'));
beforeEach(inject(function($q, _list_) {
list = _list_;
spyOn(list, 'get').and.callThrough();
list.get().then(function(result) {
labels = result;
});
}));
describe('retrieving list of labels', function() {
it('should provide a list of labels', function() {
expect(labels).not.toBe(undefined);
expect(Array.isArray(labels)).toBeTruthy();
});
});
});
However, the issue at hand is that the callback within the then function isn't being executed, despite the fact that the get method in the service returns a promise. Am I overlooking something here? I came across information about the callFake method in Jasmine, but frankly speaking, I fail to see its purpose. Can you elaborate on the advantages of using it? By the way, I am using Jasmine 2.0 along with the latest version of Angular and angular-mocks.