为什么我的业力考试不起作用?

jmo*_*789 7 angularjs karma-runner

我有这个简单的测试:

    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();
  });
});
Run Code Online (Sandbox Code Playgroud)

控制器看起来像这样:

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;
}]);
Run Code Online (Sandbox Code Playgroud)

测试保持失败,终端并没有真正提供任何有用的信息.

ale*_*cxe 0

不要this在您的测试中使用,它指的是套件不同部分中的不同事物。

相反,在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', {
        '$scope': this.scope,
      });
    });
  });

  it('should have a controller', function() {
    expect(scope.controller).toBeDefined();
  });
});
Run Code Online (Sandbox Code Playgroud)