我添加了事件广播和发射,现在jasmine无法加载元素。
控制器功能,谁接收事件,我想测试看起来像这样
function Tree(scope, $http, dataservice, Block, events) {
var $scope = scope;
init();
function init(){
// React to global events
$scope.$on(events.reloadOnNewData, onNewData);
}
在Jasmine中,我在单元测试中有以下代码:
beforeEach(inject(function (_$controller_, _ParameterList_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
controller = $controller('Tree', {$scope: $scope});
}));
并且在业力中我会使用流动的角度来实现
'./bower_components/jquery/dist/jquery.js',
'./bower_components/angular/angular.js',
'./bower_components/angular-mocks/angular-mocks.js',
'./bower_components/angular-animate/angular-animate.js',
'./bower_components/angular-route/angular-route.js',
'./bower_components/angular-sanitize/angular-sanitize.js',
当我运行测试时,我得到了
TypeError: $scope.$on is not a function
答案 0 :(得分:4)
你需要实例化rootScope并在控制器中传递它然后它不会显示这个错误。
var $scope;
beforeEach(inject(function (_$controller_, _ParameterList_,$rootScope) {
// The injector unwraps the underscores (_) from around the parameter names when matching
$scope = $rootScope.$new();
$controller = _$controller_;
controller = $controller('Tree', {$scope: $scope});
}));
答案 1 :(得分:1)
以这种方式定义我。
var scope;
beforeEach(inject(function (_$controller_, _ParameterList_,$rootScope) {
$controller = _$controller_;
scope = $rootScope.$new()
controller = $controller('Tree', {$scope: scope});
}));