如何与Jasmine测试棱角分明的承诺
How to test angular promise with Jasmine
下面的测试。当我尝试用Jasmine测试它时,console.login永远不会被调用。代码有什么问题,是与angular还是jasmine有关?
describe("angular test", function() {
var $q;
beforeEach(function() {
angular.mock.inject(function getDependencies(_$q_) {
$q = _$q_;
});
});
it("angular nested promise with jasmine", function(done) {
$q(function(resolve, reject) {
resolve(10);
})
.then(function(r) {
console.log(r);
})
.finally(function() {
done();
});
});
})
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.5/angular-mocks.js"></script>
谢谢Merott,这解决了问题。
第二个问题:
如果我正在测试返回承诺的服务。承诺包含在服务中。我还需要添加该摘要以使其发挥作用。这种消化物是茉莉花需要的,还是正常情况下需要的?(我指的是服务。)
必须通过注入$rootScope
并调用$rootScope.$digest()
来触发摘要。
describe("angular test", function() {
var $q, $rootScope;
beforeEach(function() {
angular.mock.inject(function getDependencies(_$q_, _$rootScope_) {
$q = _$q_;
$rootScope = _$rootScope_;
});
});
it("angular nested promise with jasmine", function(done) {
$q(function(resolve, reject) {
console.log('hello');
resolve(10);
}).then(function(r) {
console.log(r);
}).finally(function() {
done();
});
$rootScope.$digest();
});
})
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.5/angular-mocks.js"></script>
相关文章:
- 我的职位回报太快了,如何做出承诺
- 打破承诺链的好方法是什么
- 从函数返回角度承诺
- 使用jasmine模拟对服务器的调用
- 我怎样才能获得承诺的价值
- 测试嵌套AngularJS与Jasmine的承诺
- jasmine 2.0测试angularjs的工厂方法,这是一个承诺
- Jasmine:测试返回的承诺是一个具体的例外
- 使用 Jasmine 2.4.* 测试返回承诺的服务
- 如何使用Jasmine测试AngularJS承诺返回的内容
- 在 Nodejs 中使用 Jasmine 测试承诺是否被解析或拒绝
- 如何与Jasmine测试棱角分明的承诺
- 在自定义的Jasmine匹配器中解决和拒绝承诺
- 如何用Jasmine 2.0测试ES6承诺
- 测试的承诺.然后是Jasmine 2
- 如何在单元测试中通过jasmine spy向angular控制器返回被拒绝/失败的承诺
- AngularJS - WebSQL -在Karma中用Jasmine测试返回多个承诺的对象
- 为什么这个单元测试失败了?(Jasmine/AngularJS的承诺)
- 测试AngularJS在Jasmine 2.0中的承诺
- 有条件地解决或拒绝$q承诺,使用 Jasmine 进行单元测试