【发布时间】:2016-02-10 11:26:46
【问题描述】:
我有一个小业力单元测试的问题,它应该检查一个简单的解密/加密服务。 问题是,如果我将以下代码称为“手动”(即在我正在运行的 Angular 应用程序中),一切都很好,我会收到预期的测试输出:
this.encryptDataAsync('Hello World of Encryption','b4b63cd1a64dbef72fefe2eb3e3fc3eb').then((encryptedValue : string) : void => {
console.log('1',encryptedValue);
this.decryptDataAsync(encryptedValue,'b4b63cd1a64dbef72fefe2eb3e3fc3eb').then(function(decryptedValue : string) : void{
console.log('2',decryptedValue);
});
});
只要我尝试运行这个 Karma/Jasmine 单元测试
describe('simple encryption/decryption', function() {
var results = '';
beforeEach(function(done) {
_cryptoService.encryptDataAsync('ABC','b4b63cd1a64dbef72fefe2eb3e3fc3eb').then(function (encryptedValue){
console.log('1');
_cryptoService.decryptDataAsync(encryptedValue,'b4b63cd1a64dbef72fefe2eb3e3fc3eb').then(function(decryptedValue){
console.log('2');
results = decryptedValue;
done();
});
});
});
it("check results", function(done){
expect(results).toBe('ABC');
done();
}, 3000);
});
我从来没有到达 console.log('1') 也没有 '2'。我可以在调试单元测试时确认这一点。但是,这是整个套件中唯一失败的单元测试,所以我想这不会是模块等问题。
我的测试用例是否存在一般问题?我本来希望我可以使用 then 函数来处理我的测试用例,然后调用 done() 函数来调用断言部分。
更新/编辑: 该服务使用 webcrypto 作为库。除了作为角度服务之外,它完全独立于角度(因此,范围没有变量等)
【问题讨论】:
-
Angular 承诺在 $scope.$apply() 被调用时得到解决。而且我们不知道该服务是如何实现的。
-
这是否意味着当我调用 $scope.apply 时,$q(作为一个独立的 Angular 服务)承诺也会得到解决?如果是这种情况,$q.defer(); 之间的联系在哪里?和 $scope?
标签: angularjs unit-testing jasmine