为什么要赢得我的业力测试工作?

时间:2016-07-11 13:45:11

标签: 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();
  });
});

控制器如下所示:

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;
}]);

测试仍然失败,终端并没有真正提供任何有用的信息。

1 个答案:

答案 0 :(得分: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();
  });
});