小编der*_*ata的帖子

测试AngularJS服务,它返回一个promise而不调用$ rootScope.$ apply()?

我有一个返回promise的AngularJS服务.

尽管代码运行得很好,但测试给我带来了一些困难,因为承诺的"then"方法在我的单元测试中从未调用过.

常见的答案似乎是$rootScope.$apply()在" JasmineJS测试中未触发的AngularJS Promise Callback "一文中提到的调用.但是,如果我这样做,我的测试会尝试加载templates/home.html,这是不期望的:

PhantomJS 1.9.7 (Linux) Controller: ScanCtrl should invoke the barcode scanner when it is available FAILED
    Error: Unexpected request: GET templates/home.html
    No more request expected
Run Code Online (Sandbox Code Playgroud)

如果我没有包含$rootScope.$apply(),那么promise的"then"方法永远不会被调用,并且我得到一个错误,我的间谍没有被按预期调用:

PhantomJS 1.9.7 (Linux) Controller: ScanCtrl should invoke the barcode scanner when it is available FAILED
    Expected spy go to have been called with [ 'enterQuantity', { barcodeId : '888888888888' } ] but it was never called.
Run Code Online (Sandbox Code Playgroud)

所以我的问题是 - 如何让这些测试工作?有什么方法可以避免打电话$rootScope.$apply()吗?或者,我是否需要找到一种让我的代码templates/home.html …

javascript jasmine angularjs ionic-framework

4
推荐指数
1
解决办法
3212
查看次数

标签 统计

angularjs ×1

ionic-framework ×1

jasmine ×1

javascript ×1