24

angularJSのテストにジャスミンを使用しています。私の見解では、「Controller as」構文を使用しています。

<div ng-controller="configCtrl as config">
    <div> {{ config.status }} </div>
</div>

ジャスミンでこれらの「スコープ」変数を使用するにはどうすればよいですか? 「Controller as」とは何を指しますか? 私のテストは次のようになります。

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");
    });
}); 

scope.status確かに、呼び出しは次のエラーで終了します。

Expected undefined to be "any".

UPDATE : コントローラー (TypeScript からコンパイルされた JavaScript) は次のようになります。

var ConfigCtrl = (function () {
    function ConfigCtrl($scope) {
        this.status = "any";
    }
    ConfigCtrl.$inject = ['$scope'];
    return ConfigCtrl;
})();
4

2 に答える 2

15

構文を使用している場合、controller as$rootScope をテストに挿入する必要はありません。以下は問題なく動作するはずです。

describe('ConfigCtrl', function(){
    beforeEach(module('busybee'));

    var ctrl;

    beforeEach(inject(function($controller){
        ctrl = $controller('ConfigCtrl');
    }));

    it('should have text = "any"', function(){
         expect(ctrl.status).toBe("any");
    });
});
于 2015-02-23T11:17:52.627 に答える