ReferenceError:describe是没有定义NodeJs的
我正在尝试定义一些端点,并使用nodejs进行testing。 在server.js我有:
var express = require('express'); var func1 = require('./func1.js'); var port = 8080; var server = express(); server.configure(function(){ server.use(express.bodyParser()); }); server.post('/testend/', func1.testend);
和func1.js :
var testend = function(req, res) { serialPort.write("1", function(err, results) { serialPort.write("2" + "\n", function(err, results) { }); }); }); exports.testend = testend;
现在在test.js我试图使用这个端点:
var should = require('should'); var assert = require('assert'); var request = require('supertest'); var http = require('http'); var app = require('./../server.js'); var port = 8080; describe('Account', function() { var url = "http://localhost:" + port.toString(); it('test starts', function(done) { request(url).post('/testend/') // end handles the response .end(function(err, res) { if (err) { throw err; } res.body.error.should.type('string'); done(); }); }); });
但是当我运行node test.js我得到这个错误:
describe('Account',function(){
^
ReferenceError:describe没有定义
在对象。 (/test/test.js:9:1)
在Module._compile(module.js:456:26)
在Object.Module._extensions..js(module.js:474:10)
在Module.load(module.js:356:32)
在Function.Module._load(module.js:312:12)
在Function.Module.runMain(module.js:497:10)
在启动(node.js:119:16)
在node.js:906:3
我该如何解决这个问题?
假设你正在通过mocha进行testing,你必须使用mocha命令而不是node可执行文件运行testing。
所以,如果你还没有,请确保你做npm install mocha -g 。 然后在项目的根目录下运行mocha 。
要运行node / npmtesting而不在全局安装Mocha,可以这样做:
•将Mocha本地安装到您的项目中( npm install mocha --save-dev )
•可select安装断言库( npm install chai --save-dev )
•在您的package.json ,为scripts添加一个部分并定位mocha二进制文件
"scripts": { "test": "node ./node_modules/mocha/bin/mocha" }
把你的spec文件放到你的根目录下名为/test的目录下
•在您的规格文件中,导入断言库
var expect = require('chai').expect;
•您不需要导mocha.setup ,运行mocha.setup或调用mocha.run()
然后从您的项目根目录运行脚本:
npm test
你也可以这样做:
var mocha = require('mocha') var describe = mocha.describe var it = mocha.it var assert = require('chai').assert describe('#indexOf()', function() { it('should return -1 when not present', function() { assert.equal([1,2,3].indexOf(4), -1) }) })
参考: http : //mochajs.org/#require
OP询问从不是来自mocha node运行。 这是一个非常常见的用例,请参阅以编程方式使用Mocha
这是注入描述和它进入我的testing。
mocha.ui('bdd').run(function (failures) { process.on('exit', function () { process.exit(failures); }); });
我试图像在文档TDDD,但没有奏效,BDD虽然工作。