lundi 25 mai 2015

how to construct unit test for controller with $state?

Trying to write a unit test for my controller:

 app.controller('StartGameCtrl', function ($scope, $timeout,$state) {
      $scope.startGame = function () {
        $scope.snap = false;
        $scope.dealCards();
        debugger;
        $state.go('cpu');
      }
    });

I wrote this jasmine unit test:

describe('snap tests', function() {
  beforeEach(module('snapApp'));
  var scope, createController, state;

  beforeEach(inject(function ($rootScope, $controller,$state) {
    scope = $rootScope.$new();
    createController = function () {
      return $controller('StartGameCtrl', {
        '$scope':scope,
        '$state':state
      });
    };
  }));


  it('startGame should call dealcards', function () {
    var controller = createController();
    spyOn(scope, 'dealCards');
    scope.startGame();
    //expect(scope.dealCards).toHaveBeenCalled();
  });

});

when I run my karma test I get an error:

TypeError: 'undefined' is not an object (evaluating '$state.go')
at startgamectrl.js:9

Aucun commentaire:

Enregistrer un commentaire