angularjs – 在Angular 1.5中将模块注入karma测试

我正在尝试将我的Angular应用程序从1.3.x升级到1.5.1.我有一套测试,当我在Angular的1.3.x版本时,它与Karma PhantomJS完全一致,但是一旦升级,我的所有测试都失败了.似乎我在不再运行之前将模块注入单元测试的方式.

这在1.5中失败:

'use strict'
 App = null

 fdescribe 'App Model', ->
   beforeEach module('MyAngularApp')

   beforeEach inject ($injector)->
     App = $injector.get('App')

 it 'should exist', ->
   console.log 'App:', App
   expect(App).toBeDefined()

我也试过注射以下内容

 beforeEach inject ($injector, _App_)->
   App = _App_

但我的App模型仍然没有被注入.

我一直在挖掘AngularJS 1.5.1上的文档,但我没有看到我需要对注入器进行任何更改.

在Angular 1.5.x中,如何将模型正确地注入单元测试中?

最佳答案 好吧,看起来问题实际上是升级导致第二个角度模拟副本捆绑到应用程序中.我不确定这是来自AngularJS,Karma还是PhantomJS,但是从karma.conf.js配置中包含的文件中删除angular-mock.js文件解决了这个问题.看起来PhantomJS错误日志似乎没有提供足够的洞察力来解决实际问题.

对于遇到此问题的其他人,我注意到将我正在测试的浏览器更改为Chrome(而不是PhantomJS 2)会导致出现不同的错误消息:

Error: [$injector:modulerr] Failed to instantiate module ng due to:
Error: [$injector:modulerr] Failed to instantiate module ngLocale due to:
RangeError: Maximum call stack size exceeded

这引出了我的解决方案:https://github.com/angular/angular.js/issues/11303

点赞