我试图使用业力对我的Angularjs代码进行单元测试。 这是我的conf文件:
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
但是,如果我用jQuery代码替换javascript代码:
$("#case").css("display","none");
然后,测试成功,之前的错误消失。
你知道为什么我会收到这个错误吗?
谢谢!
答案 0 :(得分:1)
可能因为您的文档没有id为“case”的元素。
上的文档 另一方面,如果没有元素as mentioned in this stackoverflow post,则jQuery不会返回null。因此没有错误。