私のコードでES6 PromisesとAngular promisesが混在していて、それが通過するユニットテストを書くことができません。
このコードスニペットは、ジャスミンのユニットテストが失敗する2つのインスタンスを示していますが、コードは、生産の正常に動作します:
// An angular $q promise
var f1 = function() {
return $q(function(resolve, reject) {
resolve('This is function 1!');
});
}
// An ES6 promise
var f2 = function() {
return new Promise(function(resolve, reject) {
resolve('This is function 2!');
});
}
// An angular $q.all() promise, attempting to resolve a $q and ES6 promise.
var s1 = function() {
return $q.all([f1(), f2()]).then(function() {
return '$q resolved both promises!'
});
}
// An ES6 promise attempting to resolve a $q and an ES6 promise.
var s2 = function() {
return Promise.all([f1(), f2()]).then(function() {
return 'ES6 resolved both promises!'
});
}
テストは次のようになります。
describe('Testing mixed ES6 promises and Angular $q', function() {
var $scope = null;
var service = null;
//you need to indicate your module in a test
beforeEach(module('plunker'));
beforeEach(inject(function($rootScope, _testService_) {
$scope = $rootScope.$new();
service = _testService_;
}));
afterEach(function() {
});
it('should resolve f1', function(done) {
var t1 = service.f1();
t1.then(function() {
done();
});
$scope.$apply();
});
it('should resolve f2', function(done) {
var t1 = service.f1();
t1.then(function() {
done();
});
$scope.$apply();
});
it('should resolve s1', function(done) {
var t1 = service.s1();
t1.then(function() {
done();
});
$scope.$apply();
});
it('should resolve s2', function(done) {
var t1 = service.s2();
t1.then(function() {
done();
});
$scope.$apply();
});
});
このPlunkerは、作業のデモンストレーションを持っています:
最初の2つのテストは、簡単なES6または$ q promi ses。
次に、私はES6と$ qの約束を別の方法で混ぜるので、他のテストが失敗することに注意してください。
最後に、コントローラーで2つのFAILINGテストが実稼動中であることを実証しています。
なぜAngularは私のテストでES6と$ qの約束事を混ぜ合わせませんが、プロダクションコードには問題はありません。
問題を開始しようとしましたか?たぶんそれはバグで、あなたはそれを再現するための非常に簡単な方法を設計しました。 – atoth