我正在尝试使用 Karma 对 AngularJS 进行一些基本的单元测试。我编写的所有测试在语法上似乎都是正确的。但是我在最基本的步骤中遇到了问题,即代码的 beforeEach 部分。当我尝试运行测试时,出现以下问题
TypeError: Cannot read property '$injector' of null
at Object.workFn (http://localhost:9876/absolute/Users/vesriram/Documents/AngularJS%20project/vendor/js/angular-mocks.js:1698:15)
at Object.<anonymous> (http://localhost:9876/adapter/lib/angular-scenario.js:26360:54)
at Array.forEach (native)
at Object.forEach (http://localhost:9876/adapter/lib/angular-scenario.js:9593:11)
在过去的 36 小时里,我一直在尝试解决这个问题,但到目前为止还没有运气。据我所知,似乎没有其他人遇到这个问题。这让我觉得我可能在做一些愚蠢的事情。我真的很感激你们能给我的任何帮助。我很乐意发布您需要的任何其他代码(只要我可以自由地透露它)。
相关代码如下——
应用程序.js
var sell_page = angular.module("sell_page", ['ui.bootstrap']);
sell_page.controller('ItemTitleController', ['$scope','listingInformationService', '$location',function($scope, listingInformationService, $location) {
$scope.itemNames = getAllItemNames();
$scope.draftItems = getAllSavedDrafts();
document.getElementById("categorySelection").style.visibility = "hidden";
------bunch of code-------
}]);
controllersSpec.js
describe("Unit: Testing Controllers", function() {
beforeEach(angular.mock.module('sell_page'));
it('should have a ItemTitleController controller', function() {
expect(sell_page.ItemTitleController).not.to.equal(null);
});
describe("ItemTitleController", function() {
var scope;
beforeEach(angular.mock.module('sell_page'));
beforeEach(angular.mock.inject(function($rootScope, listingInformationService, $location, $controller) {
var scope = $rootScope.$new();
var controller = $controller('ItemTitleController', {
$scope : scope
});
}));
it("should display xxx properly", function() {
--some code---
});
});
业力.conf.js
basePath = '';
files = [
JASMINE,
JASMINE_ADAPTER,
'../vendor/js/angular.min.js',
'../vendor/js/angular.js',
'../vendor/js/angular-mocks.js',
'../vendor/js/angular-scenario.js',
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'../app/js/*.js',
'e2e/*.js',
'midway/*.js',
'unit/*.js',
];
exclude = [
];
reporters = ['progress'];
port = 9876;
runnerPort = 9100;
colors = true;
logLevel = LOG_INFO;
autoWatch = true;
browsers = ['Chrome'];
captureTimeout = 60000;
singleRun = false;