我正在使用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"); }); });
Expected undefined to be "any".
更新:Controller(从TypeScript编译的javascript)如下所示:
var ConfigCtrl = (function () { function ConfigCtrl($scope) { this.status = "any"; } ConfigCtrl.$inject = ['$scope']; return ConfigCtrl; })();
解决方案是在您的测试中实例化控制器时使用“控制器”语法。特别:
原文链接:https://www.f2er.com/angularjs/145146.html$ controller(‘configCtrl as config’,{$ scope:scope});
expect(scope.config.status).toBe(“any”);
以下应通过:
describe('ConfigCtrl',function(){ var scope; beforeEach(angular.mock.module('busybee')); beforeEach(angular.mock.inject(function($controller,$rootScope){ scope = $rootScope.$new(); $controller('configCtrl as config',function(){ expect(scope.config.status).toBe("any"); }); });