jbenowitz
jbenowitz

Reputation: 1815

Calling $scope methods in jasmine

I'm extremely new to angular and jasmine, so I'm sorry if this is a basic question and I've overlooked something in the documentation and tutorials I've read. I'm creating my first unit test for a new angular project. I'm running into troubles calling methods and some variables.

I'm testing this dummy controller:

.controller( 'DummyCtrl', function DummyCtrl($scope){
    $scope.name = "TBD";

    $scope.changeToJackie = function(){
        $scope.name="Jackie";
    };

    $scope.changeToGeorge = function() {
        $scope.name="George";
    };
})

Using this unit test:

describe( 'DummyCtrl', function(){
  var $scope, $controller, DummyCtrl;

  beforeEach(module('mApp');
  beforeEach(inject(function($injector) {
             $scope = $injector.get('$rootScope');
             $controller = $injector.get('$controller');
             DummyCtrl = $controller('DummyCtrl', {$scope: $scope});

  it('should start with TBD', function(){
        expect($scope.name).toEqual("TBD");
   });

  $scope.changeToJackie();
  it('should now say Jackie', function() {
      expect($scope.name).toEqual("Jackie");
   });

  $scope.changeToGeorge();
  it ('should now say George', function() {
      expect($scope.name).toEqual("George");
  });
});

I'm getting a ton more errors in this more basic version I created to figure out my actual unit tests (specifically calling a $scope.method("param") that calls an $http.PUT. My errors look more like :

Chrome 27.0 (Mac) unauth controllers DummyCtrl should start with TBD FAILED
Error: Argument 'DummyCtrl' is not a function, got undefined
    at assertArg (/<path>/build/angular/angular.js:975:11)
    at assertArgFn (/<path>/build/angular/angular.js:985:3)
    at /<path>/build/angular/angular.js:4656:9
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:20:16)
    at Object.invoke (/<path>/build/angular/angular.js:2820:28)
    at workFn (/o<path>/build/angular/angular-mocks.js:1780:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (/<path>/build/angular/angular-mocks.js:1766:25)
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:10:14)
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:9:2)
    at /o<path>/src/app/unauth/unauth.spec.js:1:1
Expected undefined to equal 'TBD'.
Error: Expected undefined to equal 'TBD'.
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:25:25)
Chrome 27.0 (Mac) unauth controllers DummyCtrl encountered a declaration exception FAILED
TypeError: Cannot read property 'stack' of null
    at workFn (/<path>/build/angular/angular-mocks.js:1782:55)
TypeError: Cannot call method 'changeToJackie' of undefined
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:28:11)
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:23:3)
    at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:9:2)

Please excuse my nativity in angular/jasmine/javascript. Any help will be grateful, even if it's just a link to a better tutorial.

Upvotes: 2

Views: 4180

Answers (1)

Karen Zilles
Karen Zilles

Reputation: 7671

You can't write testing code in a test description outside of "beforeEach" and "it" functions.

This:

  $scope.changeToJackie();
  it('should now say Jackie', function() {
      expect($scope.name).toEqual("Jackie");
   });

  $scope.changeToGeorge();
  it ('should now say George', function() {
      expect($scope.name).toEqual("George");
  });

Needs to look like:

describe('calling changeToJackie',function () {

    beforeEach(function() {
        $scope.changeToJackie();
    });

    it('should now say Jackie', function() {
        expect($scope.name).toEqual("Jackie");
    });

    it ('should say George if I call changeToGeorge', function() {
        $scope.changeToGeorge();
        expect($scope.name).toEqual("George");
    }); 
});

Upvotes: 3

Related Questions