大佬教程收集整理的这篇文章主要介绍了angularjs – 如何使用Jasmine监视Angular承诺链,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有以下示例服务:
角度服务
class Exampleservice stePDAta: [] constructor: (@$http) -> attachScopeMethod: (@scopE) -> @scope.callSteps = => @step1().then -> @step2() step1: -> @$http.get('app/step/1').then (results) => @stePDAta[0] = results.data results step2: -> @$http.get('app/step/2').then (results) => @stePDAta[2] = results.data results
此服务允许我将方法callSteps()附加到范围.调用此方法时,会对第三方API执行一系列异步$http调用.
茉莉花规格
ddescribe 'Exampleservice',-> beforeEach -> module 'myApp' beforeEach inject ($rootScope,$injector) -> @scope = $rootScope.$new() @exampleservice = $injector.get 'exampleservice' @q = $injector.get '$q' describe 'process example steps',-> beforeEach -> @exampleservice.attachScopeMethod(@scopE) it "should attach the scope method",-> expect(@scope.callSteps).toBeDefined() describe 'when called should invoke the promise chain',-> it "should call step1 and step2",-> defer = @q.defer() @exampleservice.step1 = jasmine.createSpy('step1').andReturn(defer.promisE) @exampleservice.step2 = jasmine.createSpy('step2') @scope.callSteps() defer.resolve() expect(@exampleservice.step1).toHaveBeenCalled() expect(@exampleservice.step2).toHaveBeenCalled()
该测试的结果如下:
> expect(@ exampleservice.step1).toHaveBeenCalled() – PASS
> expect(@ exampleservice.step2).toHaveBeenCalled() – 失败
你能告诉我如何让step2()在测试中成功运行吗?
谢谢
编辑
@Dashu以下提供了问题的答案.诀窍是简单地调用scope.$apply或scope.$digest来触发promise链解析.
所以这是工作测试片段.
describe 'when called should invoke the promise chain',-> it "should call step1 and step2",-> defer = @q.defer() defer.resolve() @exampleservice.step1 = jasmine.createSpy('step1').andReturn(defer.promisE) @exampleservice.step2 = jasmine.createSpy('step2') @scope.callSteps() @scope.$apply() expect(@exampleservice.step1).toHaveBeenCalled() expect(@exampleservice.step2).toHaveBeenCalled()
以上是大佬教程为你收集整理的angularjs – 如何使用Jasmine监视Angular承诺链全部内容,希望文章能够帮你解决angularjs – 如何使用Jasmine监视Angular承诺链所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。