2
으로 해결했습니다. $ q를 사용하는 Angular 코드를 테스트하는 경우 다음과 같이 즉시 해결됩니다.
angular.module('MyApp.myModule', ['ng'])
.service('someService', function($q) {
this.task = function() {
var deferred = $q.defer();
deferred.resolve('some value');
return deferred.promise;
};
});
다음과 같이 사용할 수 있습니다.
function(someService) {
someService.task().then(function() {
console.log('resolved');
});
}
응용 프로그램에서 예상대로 실행되지만 테스트에서는 실패 할 수 있습니다.
PhantomJS 1.9.7 (Mac OS X) MyApp.myModule someService someService.task when invoked returned promise when invoked should call our handler immediately FAILED
Expected spy onTaskComplete to have been called with [ 'some value' ] but it was never called.
위의 모듈에 대한 예제 테스트입니다. 이것이 실패
describe('MyApp.myModule', function() {
describe('someService', function() {
beforeEach(function() {
var suite = this;
module('MyApp.myModule');
suite.injectService = function() {
inject(function(someService) {
suite.someService = someService;
});
};
});
describe('when instantiated', function() {
beforeEach(function() {
this.injectService();
});
it('should expose the expected API', function() {
expect(typeof this.someService.task).toEqual('function');
});
});
describe('someService.task', function() {
describe('when invoked', function() {
beforeEach(function() {
this.injectService();
this.taskPromise = this.someService.task();
});
it('should return a promise', function() {
expect(typeof this.taskPromise.then).toEqual('function');
});
describe('returned promise', function() {
describe('when invoked', function() {
beforeEach(function() {
this.onTaskComplete = jasmine.createSpy('onTaskComplete');
this.taskPromise.then(this.onTaskComplete);
});
it('should call our handler immediately', function() {
expect(this.onTaskComplete).toHaveBeenCalledWith('some value');
});
});
});
});
});
});
});