Ho un problema in cui il mio codice mescola le promesse ES6 con le promesse di Angular e funziona in produzione non riesco a scrivere test unitari che superino.
Questo frammento di codice dimostra due casi in cui i test di unità Jasmine fallirà, ma il codice funziona bene in produzione:
// 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!'
});
}
I test assomigliano:
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();
});
});
Questo Plunker ha una dimostrazione di lavoro : http://plnkr.co/edit/xhRc7O
Si noti che i primi 2 test passano perché sono diretti ES6 o $ q promi SES.
Poi si noti che ogni altro test fallisce perché mescolo ES6 e $ q promette in modi diversi.
Infine, si noti che nel controller mostro che due test di FAILING funzionano effettivamente in produzione.
Perché Angular non mi permette di mischiare ES6 e $ q promette nei miei test, ma non ho problemi nel codice di produzione?
Hai provato ad iniziare in uscita? Forse è un bug e tu hai progettato un modo molto semplice per riprodurlo. – atoth