我正在嘗試使用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小時,到目前爲止有沒有運氣。據我所知,沒有人似乎有這個問題。這讓我覺得我可能做了一些愚蠢的事情。我真的很感激任何人們可以給我的幫助。我會很高興發佈你需要的任何附加代碼(只要我可以隨意泄露它)。
相關的代碼是以下各項
app.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---
});
});
karma.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;
ANGULAR_SCENARIO和ANGULAR_SCENARIO_ADAPTER是端到端的測試,從而不必列入。我不認爲這是你的問題雖然。 –
是的,我也有e2e測試。我沒有包括他們,因爲他們工作得很好,與我目前的問題無關。 :) – user2438247
你有沒有解決你的問題? – Chad