我正在使用 jasmine 进行 angularJS 测试。在我看来,我使用的是“Controller as”语法:
<div ng-controller="configCtrl as config">
<div> {{ config.status }} </div>
</div>
如何在茉莉花中使用这些“范围”变量?“控制器为”指的是什么?我的测试如下所示:
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".
更新:控制器(从 TypeScript 编译的 javascript)看起来像这样:
var ConfigCtrl = (function () {
function ConfigCtrl($scope) {
this.status = "any";
}
ConfigCtrl.$inject = ['$scope'];
return ConfigCtrl;
})();