我有这个简单的测试:
describe('My Controller',function() { beforeEach(function() { module('myApp'); return inject(function($injector) { var $controller = $injector.get('$controller'); this.rootScope = $injector.get('$rootScope'); this.scope = this.rootScope.$new(); this.controller = $controller('MyCtrl',{ '$scope': this.scope,}); }); }); it('should have a controller',function() { expect(this.controller).toBeDefined(); }); });
控制器看起来像这样:
angular.module('myApp').controller('MyCtrl',['$scope','$state','$filter','$q','BookingService','ngToast','$uibModal',function($scope,$state,$filter,$q,BookingService,ngToast,$uibModal) { $scope.bs = BookingService; $scope.roundTrip = false; $scope.reservationDetails = {}; $scope.originAddress = false; $scope.destinationAddress = false; $scope.reservationDetails.roundTrip = false; $scope.seatReservationDepart = {}; $scope.charter = false; }]);
测试保持失败,终端并没有真正提供任何有用的信息.
不要在测试中使用它,它指的是套件不同部分的不同内容.
原文链接:https://www.f2er.com/angularjs/141163.html而是在describe命名空间中初始化范围“容器”:
describe('My Controller',function() { var scope = {}; beforeEach(function() { module('myApp'); return inject(function($injector) { var $controller = $injector.get('$controller'); this.rootScope = $injector.get('$rootScope'); this.scope = this.rootScope.$new(); scope.controller = $controller('MyCtrl',function() { expect(scope.controller).toBeDefined(); }); });