我正在使用jasmine进行angularJS测试。在我的意见,我使用的“控制器”语法:如何在Jasmine中使用范围变量与“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".
UPDATE:控制器(从打字稿编译的JavaScript)是这样的:
var ConfigCtrl = (function() {
function ConfigCtrl($scope) {
this.status = "any";
}
ConfigCtrl.$inject = ['$scope'];
return ConfigCtrl;
})();
至少,你应该做'期待(scope.config.status).toBe( “任何”);' – zsong
请提供'configCtrl'的作为语法几乎是像做'$范围的代码。 config = this;'并执行'this.status =“any”;'。 –
它工作,如果我在我的控制器中手动定义'$ scope.config = this;'。但我认为这不是它应该的方式,不是吗? – 3x14159265