噶和Jasmine:找不到变量:需要

问题描述:

这是我第一次试图单元测试一些代码,我使用的是噶和Jasmine时收到此错误:噶和Jasmine:找不到变量:需要

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR 
ReferenceError: Can't find variable: require 
at unit/tests.js:1 

我想npm install karma-browserify --save-dev但没有解决这个问题。

任何想法如何排序?

我噶的conf文件:

// Karma configuration 
// Generated on Tue Nov 08 2016 03:14:50 GMT+0000 (GMT Standard Time) 

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: [ 
    // '../shopping-basket.js', 
    'unit/*.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/**/*.js': ['browserify'] 
}, 


// 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_INFO, 


// 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: ['PhantomJS'], 
//browsers: ['Chrome'], 


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

// Concurrency level 
// how many browser should be started simultaneous 
concurrency: Infinity 
}) 
} 

tests.js

var myCode = require('./shopping-basket-functions'); 

describe('tests', function(){ 
    describe('testFunction', function(){ 
     it('should return 1', function(){ 
      // Call the exported function from the module 
      myCode.testFunction().should.equal(1); 
     }) 
    }) 
}) 

购物篮,functions.js

function testFunction() { 
    return 1; 
} 

// If we're running under Node, 
if(typeof exports !== 'undefined') { 
    exports.testFunction = testFunction; 
} 

您是否尝试过使用该模板的位置:https://www.npmjs.com/package/grunt-template-jasmine-nml为在answer中指定?

长话短说,它的作用是允许您使用NodeJS使用的CommonJS语法来引用和拉入其他模块。请参见下面的你将如何引用它在你的茉莉配置文件的例子(同样,每个其他用户的答案):

jasmine: { 
    options: { 
     template: require('grunt-template-jasmine-nml'), 
     helpers: 'spec/helpers/**/*.js', 
     specs: 'spec/**/*.spec.js' 
    } 
} 

不得不试图建立一个应用程序相同的问题,phanotmjs不停蠕动的未定义的变量需要,解决方法是: npm install grunt-template-jasmine -n​​ml --save-dev

这将更新正确的模板,并在之后生效。 尝试: 咕噜声测试-dd无误完成