Rol*_*and 8 javascript unit-testing angularjs angular-promise
我正在尝试测试我构建的服务,该服务使用Angular的$ q Promise实现.我正在使用Karma,Mocha,Chai,Sinon,Sinon Chai和Chai的组合作为承诺.
我写的所有测试和返回的承诺都是通过但拒绝或使用的$q.all([ ... ]).我已经尝试了所有我能想到的但是我似乎无法找到问题所在.
以下是我正在测试的超薄版本:
"use strict";
describe("Promise", function () {
var $rootScope,
$scope,
$q;
beforeEach(angular.mock.inject(function (_$rootScope_, _$q_) {
$rootScope = _$rootScope_;
$q = _$q_;
$scope = $rootScope.$new();
}));
afterEach(function () {
$scope.$apply();
});
it("should resolve promise and eventually return", function () {
var defer = $q.defer();
defer.resolve("incredible, this doesn't work at all");
return defer.promise.should.eventually.deep.equal("incredible, this doesn't work at all");
});
it("should resolve promises as expected", function () {
var fst = $q.defer(),
snd = $q.defer();
fst
.promise
.then(function (value) {
value.should.eql("phew, this works");
});
snd
.promise
.then(function (value) {
value.should.eql("wow, this works as well");
});
fst.resolve("phew, this works");
snd.resolve("wow, this works as well");
var all = $q.all([
fst.promise,
snd.promise
]);
return all.should.be.fullfiled;
});
it("should reject promise and eventually return", function () {
return $q.reject("no way, this doesn't work either?").should.eventually.deep.equal("no way, this doesn't work either?");
});
it("should reject promises as expected", function () {
var promise = $q.reject("sadly I failed for some stupid reason");
promise
["catch"](function (reason) {
reason.should.eql("sadly I failed for some stupid reason");
});
var all = $q.all([
promise
]);
return all.should.be.rejected;
});
});
Run Code Online (Sandbox Code Playgroud)
第三次,最后一次和第一次测试是失败的.实际上它没有失败,它只是在超时超时后解决,我得到了 Error: timeout of 2000ms exceeded.
编辑:我刚刚尝试用Kris Kowal对承诺的实施进行测试,它的工作原理很好.
PS 我实际上发现在Mocha,Chai或Chai As Promised的碗里花了一些时间,并且afterEach挂钩在超时之后被调用.
Pro*_*ser 13
afterEach()用于清理,而不是在准备之后但在测试之前执行代码.$scope.$apply()也不是清理.
您需要执行以下操作:
// setup async behavior
var all = $q.all(x.promise, y.promise)
// resolve your deferreds/promises
x.reject(); y.reject();
// call $scope.$apply() to 'digest' all the promises
$scope.$apply();
// test the results
return all.should.be.rejected;
Run Code Online (Sandbox Code Playgroud)
您正在进行$apply()测试,而不是在设置和评估之间.
我试图找出为什么测试没有通过,尽管乍一看应该通过。当然,我必须$scope.$apply();从afterEach这不是 @proloser 提到的调用位置。
即使我已经这样做了,测试仍然没有通过。我还打开了关于chai-as-promised和Angular 的问题,看看我是否得到任何输入/反馈,实际上我被告知它很可能不起作用。原因可能是因为 Angular$q对摘要阶段的依赖,而 chai-as-promsied 库中没有考虑到这一点。
因此,我用Q而不是$q检查了测试,它工作得很好,从而加强了我的假设,即错误不在 chai-as-promised 库中。
我最终放弃了 chai-as-promised,并使用 Mocha 的回调重写了我的测试done(即使在幕后,chai-as-promised 也做了同样的事情):
"use strict";
describe("Promise", function () {
var $rootScope,
$scope,
$q;
beforeEach(angular.mock.inject(function (_$rootScope_, _$q_) {
$rootScope = _$rootScope_;
$q = _$q_;
$scope = $rootScope.$new();
}));
it("should resolve promise and eventually return", function (done) {
var defer = $q.defer();
defer
.promise
.then(function (value) {
value.should.eql("incredible, this doesn't work at all");
done();
});
defer.resolve("incredible, this doesn't work at all");
$scope.$apply();
});
it("should resolve promises as expected", function (done) {
var fst = $q.defer(),
snd = $q.defer();
fst
.promise
.then(function (value) {
value.should.eql("phew, this works");
});
snd
.promise
.then(function (value) {
value.should.eql("wow, this works as well");
});
fst.resolve("phew, this works");
snd.resolve("wow, this works as well");
var all = $q.all([
fst.promise,
snd.promise
]);
all
.then(function () {
done();
});
$scope.$apply();
});
it("should reject promise and eventually return", function (done) {
$q
.reject("no way, this doesn't work either?")
.catch(function (value) {
value.should.eql("no way, this doesn't work either?");
done();
});
$scope.$apply();
});
it("should reject promises as expected", function (done) {
var promise = $q.reject("sadly I failed for some stupid reason");
promise
["catch"](function (reason) {
reason.should.eql("sadly I failed for some stupid reason");
});
var all = $q.all([
promise
]);
all
.catch(function () {
done();
});
$scope.$apply();
});
});
Run Code Online (Sandbox Code Playgroud)
上述测试将按预期全部通过。可能还有其他方法可以做到这一点,但我不知道还有什么方法,所以如果其他人这样做,最好将其发布,以便其他人可以从中受益。
| 归档时间: |
|
| 查看次数: |
5846 次 |
| 最近记录: |