1

I did read the angular official tutorial and launch test with success. But I tried to make test for a simple controller (when you click on button a boolean change), i have the following 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']
  });
};

1 Answer 1

2

In the second group of test change $scope to scope

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

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

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

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

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);
      });
    });
  });
<link href="http://safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet"/>
<script src="http://safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-mocks.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

The error stay the same.But I think you have right. The '$' is a code artifact of other solution i have try : change var scope for var $scope change scope = $rootScope.$new(); to scope ={} but I don't understand my error
Thanks, it's work in a html page. I will continue to try launch with node and karma when i will have more time.
@Valtena you must check what files and witch order are loaded by Karma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.