Jam*_*son 2 synchronous promise jasmine angularjs
如果您正在使用$ 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;
};
});
Run Code Online (Sandbox Code Playgroud)
可以使用如下;
function(someService) {
someService.task().then(function() {
console.log('resolved');
});
}
Run Code Online (Sandbox Code Playgroud)
您可能会发现它在您的应用程序中按预期运行,但在测试中失败;
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.
Run Code Online (Sandbox Code Playgroud)
以下是上述模块的示例测试;
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');
});
});
});
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
失败的原因是,虽然该代码看上去是同步的,内部$q采用$evalAsync从$scope推迟工作,直到将来调用堆栈.由于$q没有flush方法一样$httpBackend,$timeout和$interval做-将呼叫$rootScope.$digest()是需要达到同样的效果.
PhantomJS 1.9.7 (Mac OS X): Executed 3 of 3 SUCCESS (0.451 secs / 0.01 secs)
Run Code Online (Sandbox Code Playgroud)
这是更新的示例测试;
describe('MyApp.myModule', function() {
describe('someService', function() {
beforeEach(function() {
var suite = this;
module('MyApp.myModule');
inject(function($rootScope) {
suite.$rootScope = $rootScope;
});
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);
this.$rootScope.$digest();
});
it('should call our handler immediately', function() {
expect(this.onTaskComplete).toHaveBeenCalledWith('some value');
});
});
});
});
});
});
});
Run Code Online (Sandbox Code Playgroud)