jeudi 7 mai 2015

Jasmine not reporting that Angular controller function has been called

I have a method defined on a controller and I'm trying to test if it has been called. I am able to test that the method is defined, but when I try and test if it has been called, it complains that it expected the spy to have been called.

Here is my controller:

myControllers.controller 'MyCtrl', ($scope) ->
  this.init = () -> doStuff()
  this.init()

Here is my test suite:

describe "testing myCtrl", () ->
  beforeEach "myControllers"
  $controller = {}
  $scope = {}

  beforeEach inject (_$controller_,$rootScope) ->
    $controller = _$controller_
    $scope = $rootScope.$new()

  describe "MyCtrl", () ->
    beforeEach () -> 
      MyCtrl=$controller('MyCtrl',{$scope:$scope})

    it "should be defined", () -> # passes
      expect(MyCtrl).toBeDefined() 

    it "should define #init", () -> # passes
      expect(angular.isFunction(MyCtrl.init)).toBe true 

    it "should call #init", () -> # fails
      spyOn MyCtrl, 'init'
      expect(MyCtrl.init).toHaveBeenCalled()

The last assertion results in Error: Expected spy init to have been called.

Aucun commentaire:

Enregistrer un commentaire