【问题标题】:Angular Mocks and Jasmine - "Argument 'fn' is not a function, got Object" errorAngular Mocks 和 Jasmine -“参数 'fn' 不是函数,得到对象”错误
【发布时间】:2017-03-30 08:02:40
【问题描述】:

我在使用 Angular(使用 Angular-Mocks)、Jasmine 和 CoffeeScript 进行单元测试时遇到问题。

具体来说,这段代码会中断:

'use strict'

describe 'sample suite', ->

    beforeEach inject(($rootScope, $compile) ->
        scope = $rootScope.$new()
    )

    it 'should be true', ->
        expect('foo').toBe('foo')

导致 Angular debug.html:37 Error: [ng:areq] Argument 'fn' is not a function, got Object 错误。

但这确实有效:

'use strict'

describe 'sample suite', ->

    beforeEach ->
        sample = 'test'

    it 'should be true', ->
        expect('foo').toBe('foo')

这意味着使用带有全局 inject() angular-mocks 方法的语法不能与 CoffeeScript 编译器一起正常工作。

遗憾的是,以 return 结尾的 beforeEach 块不起作用。

任何帮助表示赞赏。

【问题讨论】:

  • @TomePejoski 你的意思是不使用通配符吗?没有帮助,我想问题出在 Karma 和 Jasmine 适配器之间。
  • 不,it does not。您的测试代码显然与您发布的代码不同。请提供MCVE。这很可能是由于您的 Angular 应用程序中的错误(不正确的 config 块左右)而发生的。

标签: javascript angularjs unit-testing coffeescript jasmine


【解决方案1】:

如果你查看你的两个代码工作和不工作,你会发现很大的不同

在第一个块beforeEach 接受inject 作为参数,而在工作中你传递一个匿名函数beforeEach -> 作为参数 这就是为什么你得到错误Error: [ng:areq] Argument 'fn' is not a function, got Object

'use strict'

describe 'sample suite', ->

  beforeEach ->
    inject ($rootScope, $compile) ->
      scope = $rootScope.$new()


  it 'should be true', ->
    expect('foo').toBe('foo')

【讨论】:

  • 是的,有区别,不,inject 接受这两种用法,这不是这里的问题。
  • 我说的不是inject,我说的是beforeEach,它需要函数并且你用对象执行它(注入)
  • inject() 返回一个可以调用的包装函数(通过 beforeEach 或它)。这是预期的行为。见stackoverflow.com/questions/40626775/…
  • 对,我的错,除了从我的头顶上我可以想到糟糕的依赖注入,即.factory('myFactory', 'injection', function(injection){})而不是数组表示法.factory('myFactory', ['injection', function(injection){}])
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多