Angular Mocks和Jasmine - "Argument '不是一个函数,得到Object&

Angular Mocks and Jasmine - "Argument 'fn' is not a function, got Object" error

本文关键字:一个 得到 函数 Object Jasmine Mocks quot Angular Argument      更新时间:2023-09-26

我在使用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块是行不通的。

感谢您的帮助。

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

在第一个块中,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')
相关文章: