[英]Karma gives me errors when running tests (related to backbone.js)?
我的錯誤與骨干'undefined' is not an object (evaluating '_.each')
有關。 'undefined' is not an object (evaluating '_.each')
它指向的第227行位於我的test.js腳本中,該腳本由browserify編譯。
該行是骨干網中的內置塊,因此實際問題顯然不在該行之內。
因此,我的設置...我在這里測試一個簡單的模塊,它./client/src/views/
。
// intro.js
var Backbone = require('backbone');
module.exports = IntroView = Backbone.View.extend({
el: '.intro',
initialize: function() {
this.render();
},
render: function() {
this.$el.height($(window).height() - 10);
}
});
然后我在./client/spec/views/
有實際的測試模塊
// intro.test.js
var IntroView = require('../../src/views/intro.js'),
$ = require('jquery');
describe('view:intro', function() {
var introView = new IntroView();
it('just testing if I can get a success', function() {
expect(1 + 2).toEqual(3);
});
it('should render the view', function() {
introView.render();
});
});
好的,所以我運行gulp browserify
來編譯這樣的腳本
gulp.task('browserify:tests', function() {
return gulp.src('./client/spec/views/intro.test.js')
.pipe(tasks.browserify({
transform: ['hbsfy', 'browserify-shim']
}))
.pipe(tasks.concat('tests.js'))
.pipe(gulp.dest('./test'));
});
然后在我的karma.config.js
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'./test/tests.js',
],
// list of files to exclude
exclude: [
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
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_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
最后,我在命令行中運行karma start
。 因此,我希望我以正確的方式使用業力,我想是,但是如果我想測試使用骨干網的代碼,則需要克服這一難題。
所以我的問題是我在做什么錯,為什么該錯誤發生在ribs.js代碼內? 我也做對了嗎?
我有同樣的錯誤,但這是由於在我的requirejs
配置中使用模塊map
引起的。 我已經通過使用paths
解決了它(我已經覆蓋了我的瀏覽器配置)。
您的問題很可能是您沒有requirejs
配置。 有關更多說明,請參見requirejs
配置頁面 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.