2014-11-16 3 views
0

Я пытаюсь выполнить юнит-тесты моего кода Angularjs, используя карму. Это мой конф файл:Карма: невозможно прочитать свойство 'style' of undefined

module.exports = function(config) { 
config.set({ 

// base path that will be used to resolve all patterns (eg. files, exclude) 
basePath: './', 

// frameworks to use 
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
frameworks: ['jasmine'], 


// list of files/patterns to load in the browser 
files: [ 
    'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', 
    'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js', 
    'https://code.angularjs.org/1.2.26/angular-mocks.js', 
    'test/unit/**/*.js', 
    'js/app.js', 
    'js/controllers/AffairesCtrl.js' 
], 



// list of files to exclude 
exclude: [ 
], 


// preprocess matching files before serving them to the browser 
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
preprocessors: { 
}, 


// test results reporter to use 
// possible values: 'dots', 'progress' 
// available reporters: https://npmjs.org/browse/keyword/karma-reporter 
reporters: ['progress'], 


// web server port 
port: 9876, 


// enable/disable colors in the output (reporters and logs) 
colors: true, 


// level of logging 
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
logLevel: config.LOG_DEBUG, 


// enable/disable watching file and executing tests whenever any file changes 
autoWatch: true, 


// start these browsers 
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
browsers: ['Chrome'], 


// Continuous Integration mode 
// if true, Karma captures browsers, runs the tests and exits 
singleRun: false 
}); 
}; 

Я тестирует функцию, которая имеет следующий код:

document.getElementById('case').style.display="none"; 

Когда я запустить тест, я получаю эту ошибку:

TypeError: Cannot read property 'style' of undefined 

Но если я заменил код javascript кодом jQuery:

$("#case").css("display","none"); 

Затем тест проходит успешно, и предыдущая ошибка исчезает.

Знаете ли вы, почему я получаю эту ошибку?

Спасибо!

ответ

0

Вероятно, потому что ваш документ не имеет элемента с идентификатором «case».

Смотрите документацию по getElementById

JQuery с другой стороны, не возвращает нулевое значение, если ни один из элементов не существует as mentioned in this stackoverflow post. Поэтому нет ошибок.