Tag: 摩卡

摩卡正在复制对象的值,因此无法更新它

我正在运行摩卡UT框架+ supertest + chai。 我有我运行以下function: function Test1(inputObj) { return function(done) { … require .post('…') .expect(302) .end(function(err, res) { if (err) return done(err); inputObj.id = 'someIdFromResponse'; console.log('after update: ', inputObj); // inputObj includes id done(); } } function Test2(inputObj) { return function(done) { console.log('Test2.inputObj: ', inputObj); // no id is printed! done(); } } 运行以下摩卡步骤: var […]

摩卡asynchronoustesting,重用相同的请求

我正在testing一个HTTP服务器。 我希望我可以通过describe() ,而不是仅仅it() ,如下所示: var request = require('supertest'); describe('Item', function(done){ request(app).get('/').end(function(req, res){ it('should have one thing', function(){ // Assert on res.body }); it('should have another thing', function(){ // Assert on res.body }); it('should have more things', function(){ // Assert on res.body }); done(); }); }); 这是行不通的,摩卡从不运行testing。 下面的代码可以工作,但是每次创build一个新的HTTP请求时,我都希望使用相同的代码。 describe('Item', function(){ it('should have one thing', function(done){ request(app).get('/').end(function(req, […]

为所有testing用例(Mocha)添加一个默认的before()函数,

我正在使用TDD(Mocha)为我的node.js服务器编写函数。 为了连接到我正在做的数据库 before(function(done){ db.connect(function(){ done(); }); }); 我使用make test运行make test ,并configuration了我的makefile以使用mocha *.js运行该特定文件夹中的所有js文件 但是对于每个js文件,我都必须独立连接到数据库,否则我的testing用例会失败,因为他们不能与其他testing文件共享通用范围。 所以问题是,是否有像beforeAll()那样简单地连接一次到数据库,然后运行所有的testing用例? 任何帮助/build议表示赞赏。

在Node.js中,如何编写unit testing页面速度?

我正在进行网站重新平台/重新devise。 我们将使用Node.js和Hapi。 我想添加将测量页面加载速度的unit testing。 有没有办法使用摩卡和柴做到这一点? 有没有专门用于页面速度testing的框架?

如何在node.js模块中正确导出variables,以使其在unit testing中可见

我想unit testingnode.js模块,我想知道如何正确导出variables和对象? 所以我可以在我的unit testing中访问他们,并在他们的工作。 源代码: var someObject = {}; var Array = [189, 347, 497, 632, 750, 900, 995, 1137], someNumber = 142, sizeOfArray = Allowance.length; someObject = { method1 : function (info) { //something… return { someOtherMethod1: someObject.method1(info) && someObject.method2(info) someOtherMethod2: someObject.method3(info) }; }, method2 : function (info) { //something… return something2; } method3 […]

重用摩卡testing代码

我正在开发一个NodeJS应用程序,并使用Mocha进行unit testing。 比方说,我有两个非常相似的testing西装。 实际上这些testing是针对两个实现相同接口的类。 例如: suit_a.js var A = require('./a'); describe(function () { var instance; beforeEach(function () { instance = new A(); }); it(function () { assert(instance.getSomeValue() === 1); }); }); suit_b.js var B = require('./b'); describe(function () { var instance; beforeEach(function () { instance = new B({option: "option-value"}); }); it(function () { assert(instance.getSomeValue() === […]

使用Mocha和Grunt在Sails.js中运行functiontesting时的竞态条件

我正在使用Sails v0.10.x,并且在运行我的functiontesting时遇到问题。 testing/ bootstrap.test.js // force the test environment to 'test' process.env.NODE_ENV = 'test'; var Sails = require('sails'); // use zombie.js as headless browser var Browser = require('zombie'); // Global before hook before(function(done) { var self = this; // Lift Sails and start the server Sails.lift({ log: { level: 'error' }, }, function(err, sails) { […]

testingGrunt任务

我正在使用Yeoman来生成一些项目和咕-任务。 现在我也想使用Mocha来testing生成的grunt任务,但是我只能find一些如何在Grunt中使用Mochatesting的信息;-) 任何人都可以帮忙吗?

用Mocha / Sinon – SailstestingNodeJs

我对nodejs和sailsjs框架相对比较陌生,我真的很喜欢用它编写代码,但是来自PHP背景,我发现testingnodeJs有点奇怪。 我试图用mocha和sinon方法login我的AuthService ,但不是专家,我需要一个例子,你如何实现一个成功的testing,不幸的是,在线文档还是有点差,一个例子将是一个很多有用的感谢! login: function(username,password,callback) { User.findOneByUsername(username, function(err,user){ // If has some error if (err) { return callback(err) } // if the user is not found with that username if (!user) { return callback(err,false); } // if is found we match the password bcrypt.compare(password, user.password, function (err, res) { if (!res || err) return callback(err,false); […]

摩卡单位testing路线返回200好

我正在使用摩卡来testing,如果我已经添加到我的服务器的一些路线返回200好,如果响应包含一个string。 我这样做是这样的: var testDBRoute = function(link, routeName) { it('should return 200 OK for ' + routeName, function (done) { http.get(link, function (res) { assert.equal(200, res.statusCode); done(); }) }); it('should return the correct info', function(done) { http.get(link, function (res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { var response […]