4

I'm trying to write some tests for a controller, but ALL the documentation/tutorials/etc on this demonstrate with functions and variables on $scope. What if you want to test something NOT on $scope?

Ex:

app.controller('fakeCtrl', function($scope) {

    var foo = 'bar';

    function fooBar() {
        console.log('foo bar');
    }

});

Is there any way to unit test foo or fooBar()?

Here's what DOESN'T work:

describe("fake controller", function(){

    beforeEach(module('app'));

    var $controller;

    beforeEach(inject(function(_$controller_, $rootScope){
        $controller = _$controller_;
        $scope = $rootScope.$new();
    }));

    describe('foo property', function() {
        it('is bar', function() {
          var controller = $controller('fakeCtrl', { $scope: $scope });
          expect(controller.foo).toEqual('bar');
        });
    });
   ...

2 Answers 2

3

You can not. Variable foo and function fooBar are inaccessible outside the scope of your controller constructor. You can however do something like that:

app.controller('fakeCtrl', function($scope) {
  this.foo = 'bar';
  this.fooBar() {
    console.log('foo bar');
  }
});

And than:

(...)
describe('foo property', function() {
    it('is bar', function() {
      var controller = $controller('fakeCtrl', { $scope: $scope });
      expect(controller.foo).toEqual('bar');
    });
});

In the test you have described foo as a property, but in your controller foo is just variable not property.

Edit: Check https://docs.angularjs.org/api/ng/directive/ngController and controller as syntax.

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

Comments

1

There is no way in javascript to access local(declared inside function) function or variable from outside scope. You have some options to test this:

  1. Declare function/variable on scope.
  2. Move function/variable to angular service and test that service
  3. Test function/variable indirectly, by testing methods on scope that use that function/value

I prefer to move such functions/variables to angular services, but it depends on their purpose.

Comments

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.