【发布时间】:2013-08-27 19:08:16
【问题描述】:
我正在使用 jasmine 进行 angularJS 测试。在我看来,我使用的是“Controller as”语法:
<div ng-controller="configCtrl as config">
<div> {{ config.status }} </div>
</div>
如何在 jasmine 中使用这些“范围”变量? “控制器为”指的是什么? 我的测试如下所示:
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;
})();
【问题讨论】:
-
至少,你应该这样做
expect(scope.config.status).toBe("any"); -
请提供
configCtrl的代码,as语法几乎就像$scope.config = this;和this.status = "any";一样。 -
如果我在控制器中手动定义
$scope.config = this;,它就可以工作。但我认为这不是应有的方式,不是吗?