vendredi 24 avril 2015

angular test, can't find variable : $scope

I did the angular official tutorial and launch test with succes. But I try to make test for a simple controller (when you click on button a boolean change), i have the error : ReferenceError: Can't find variable: $scope

controller :

siteLogic = angular.module('siteLogic', []);

siteLogic.controller('siteCtrl', ['$scope',function($scope, initMap) {
  $scope.addingSite=false;

  $scope.changeAddingSite = function(){
    $scope.addingSite= !$scope.addingSite;
  };
}]);

file for test :

  describe('$scope initialisation', function() {

    beforeEach(module('siteLogic'));

    describe('sets the addingSite at the inverse', function() {
      var scope, controller;


      beforeEach(inject(function($controller, $rootScope) {
        scope = $rootScope.$new(); 
        //it's on the next line I have the error indication
        controller = $controller('siteCtrl', { $scope: scope }); 
      }));

      it('sets true if false', function() {
        scope.addingSite = false;
        scope.changeAddingSite();
        expect(scope.addingSite).toEqual(true);
      });

      it('sets false if true', function() {
        $scope.addingSite = true;
        $scope.changeAddingSite();
        expect($scope.addingSite).toEqual(false);
      });
    });
  });

karma.config.js

module.exports = function(config) {
  config.set({
    basePath: '..',
    frameworks: ['jasmine'],
    files: [
      'test/dep/angular-1.3.15/angular.js',
      'test/dep/angular-1.3.15/angular-resource.js',
      'test/dep/angular-1.3.15/angular-route.js',
      'test/dep/angular-1.3.15/angular-mocks.js',
      'lib/map/public/angular/*.js',
      'test/map/*.js'
    ],
    autoWatch: true,
    browsers: ['PhantomJS']
  });
};

Aucun commentaire:

Enregistrer un commentaire