包装第三方承诺时业力 $q 未解决
karma $q not resolve when wrapping a third-party promise
我正在用 $q 包装原生 js promise。在测试中,本机承诺已解决,但 $q 未解决。
我正在使用业力茉莉
it('promise', () => {
let $q, $rootScope;
inject((_$q_ : any, _$rootScope_ : any, ) => {
$q = _$q_;
$rootScope = _$rootScope_;
});
const deferred = $q.defer();
const promise = new Promise(function (resolve, reject) {
resolve('Stuff worked!');
});
promise.then(function (x) {
console.log('inside then', x); // THIS IS RESOLVING
deferred.resolve(x);
}).catch(function (x) {
deferred.reject(x);
});
deferred.promise.then((x) => {
console.log(x); // THIS IS NEVER RESOLVED
}, (y) => {
console.log(y);
});
$rootScope.$apply();
$rootScope.$digest();
});
我什至尝试将其包装在 $timeout 中...
找到解决方案。使用完成函数
fit('promise', (done) => {
let $q, $rootScope;
inject((_$q_: any, _$rootScope_: any) => {
$q = _$q_;
$rootScope = _$rootScope_;
});
const deferred = $q.defer();
const promise = new Promise(function (resolve, reject) {
resolve('Stuff worked!');
});
deferred.promise.then((x) => {
done(); // THIS WILL CAUSE KARMA T WAIT AND RESOLV THE PROMISE
//expect.......
}, (y) => {
console.log(y);
});
promise.then(function (x) {
console.log('inside then', x);
deferred.resolve(x);
$rootScope.$apply(); //MUST ADD APPLY HERE TOO
}).catch(function (x) {
deferred.reject(x);
});
$rootScope.$apply();
$rootScope.$digest();
});
我正在用 $q 包装原生 js promise。在测试中,本机承诺已解决,但 $q 未解决。 我正在使用业力茉莉
it('promise', () => {
let $q, $rootScope;
inject((_$q_ : any, _$rootScope_ : any, ) => {
$q = _$q_;
$rootScope = _$rootScope_;
});
const deferred = $q.defer();
const promise = new Promise(function (resolve, reject) {
resolve('Stuff worked!');
});
promise.then(function (x) {
console.log('inside then', x); // THIS IS RESOLVING
deferred.resolve(x);
}).catch(function (x) {
deferred.reject(x);
});
deferred.promise.then((x) => {
console.log(x); // THIS IS NEVER RESOLVED
}, (y) => {
console.log(y);
});
$rootScope.$apply();
$rootScope.$digest();
});
我什至尝试将其包装在 $timeout 中...
找到解决方案。使用完成函数
fit('promise', (done) => {
let $q, $rootScope;
inject((_$q_: any, _$rootScope_: any) => {
$q = _$q_;
$rootScope = _$rootScope_;
});
const deferred = $q.defer();
const promise = new Promise(function (resolve, reject) {
resolve('Stuff worked!');
});
deferred.promise.then((x) => {
done(); // THIS WILL CAUSE KARMA T WAIT AND RESOLV THE PROMISE
//expect.......
}, (y) => {
console.log(y);
});
promise.then(function (x) {
console.log('inside then', x);
deferred.resolve(x);
$rootScope.$apply(); //MUST ADD APPLY HERE TOO
}).catch(function (x) {
deferred.reject(x);
});
$rootScope.$apply();
$rootScope.$digest();
});