2

所以,我试图掌握测试角度,我有点卡住了......从我读过的内容(或者我从我读过的内容中理解的内容)下面应该可以工作,但是我收到以下错误:

错误:[ng:areq] 参数 'fn' 不是函数,得到对象 http://errors.angularjs.org/1.2.26/ng/areq?p0=fn&p1=not%20a%20function%2C%20got% 20对象

app = angular.module("MyApp", ["ngMock"])
myService = null

angular.module("MyApp").factory "myDependency", () ->
  getSomething: ->
    "awesome"

angular.module("MyApp").factory "myService", (myDependency) ->
  useDependency: ->
    myDependency.getSomething()

describe "myService", ->
  beforeEach ->
    module "MyApp", ($provide) ->
      mockDependency =
        getSomething: ->
          "mockReturnValue"

      console.log "providing"

      $provide.value("myDependency", mockDependency)

    inject (_myService_) ->
      console.log "injecting"
      myService = _myService_

  it "is there", ->
    expect(myService).not.toBeNull()
    expect(myService.useDependency()).toEq("mockReturnValue")

同样值得一提的是,“提供者”出现在日志中,但“注入”没有

4

1 回答 1

2

啊哈!我已经想通了!!!

module "MyApp", ($provide) ->
  mockDependency =
    getSomething: ->
      "mockReturnValue"

  console.log "providing"

  $provide.value("myDependency", mockDependency)
  return

这修复了错误!我假设如果一个模块返回一些东西,它必须是某种类型。如果它为空,则 Angular 会忽略它。惊人的!

于 2014-10-13T18:57:14.063 回答