Michael Baldry
Michael Baldry

Reputation: 2028

angular, karma dependency injection, inject failing

So, I'm trying to get to grips with testing angular, and I'm a bit stuck... From what I've read (or what I've understood from what I've read) the below should work, but I'm getting the following error:

Error: [ng:areq] Argument 'fn' is not a function, got Object http://errors.angularjs.org/1.2.26/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object

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")

Also its worth saying that "provider" appears in the log, but "injecting" doesn't

Upvotes: 2

Views: 386

Answers (1)

Michael Baldry
Michael Baldry

Reputation: 2028

Aha! I've figured it out!!!

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

  console.log "providing"

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

This fixes the error! I'm assuming if a module returns something, it must be of a certain type. If its null, angular ignores it. Awesome!

Upvotes: 2

Related Questions