gpt4 book ai didi

angularjs - 为什么我的 karma 测试不起作用?

转载 作者:行者123 更新时间:2023-12-02 11:30:20 24 4
gpt4 key购买 nike

我有这个简单的测试:

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

Controller 看起来像这样:

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

测试不断失败,终端并没有真正提供任何有用的信息来说明原因。

最佳答案

不要在测试中使用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();
});
});

关于angularjs - 为什么我的 karma 测试不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com