How to use scope variables with the "Controller as" syntax in Jasmine?

angularjs, jasmine

Solution

The solution is to use the "controller as" syntax when instantiating your controller in your test. Specifically:

$controller('configCtrl as config', {$scope: scope});

expect(scope.config.status).toBe("any");

The following should now pass:

describe('ConfigCtrl', function(){
    var scope;

    beforeEach(angular.mock.module('busybee'));
    beforeEach(angular.mock.inject(function($controller,$rootScope){
        scope = $rootScope.$new();

        $controller('configCtrl as config', {$scope: scope});
    }));

    it('should have text = "any"', function(){
        expect(scope.config.status).toBe("any");
    });
}); 

Problem

I'm using jasmine for angularJS testing. In my views, I'm using the "Controller as" syntax: ``` <div ng-controller="configCtrl as config"> <div> {{ config.status }} </div> </div> ``` How can I use these "scope" variables in jasmine? What does the "Controller as" refer to? My test looks like following: ``` describe('ConfigCtrl', function(){ var scope; beforeEach(angular.mock.module('busybee')); beforeEach(angular.mock.inject(function($rootScope){ scope = $rootScope.$new(); $controller('configCtrl', {$scope: scope}); })); it('should have text = "any"', function(){ expect(scope.status).toBe("any"); }); }); ``` Calling `scope.status` ends, for sure, with the error: ``` Expected undefined to be "any". ``` UPDATE: Controller (compiled javascript from TypeScript) looks like this: ``` var ConfigCtrl = (function () { function ConfigCtrl($scope) { this.status = "any"; } ConfigCtrl.$inject = ['$scope']; return ConfigCtrl; })(); ```

Original source