Tag: unit testing

在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() === […]

生成一个无效的文件名称用于testing目的

我有一段我需要testing的代码。 其中一个testing需要添加覆盖,其中stream.Writable创build给出一个指定的output文件名。 我需要testingWritable应该失败的情况,它应该发出error事件并执行适当的error handling函数等。 现在,我想我只应该创build一个无效的文件,使用一些奇怪的名字。 但是这个任务似乎比预期的更难一些。 我怎样才能自动将这段代码片段放入catch块? outputFile = String(outputFile); // not exactly how this is really done, // but the end result is the same try { writer = fs.createWriteStream(outputFile, { flags: 'w' }) .on('error', errorHandler) // basically throws the error .on('close', closeHandler); // function 100% tested } catch (e) { // irrelevant code […]

用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); […]

如何在node.js + Express.js + mongodb应用程序上asynchronous启动摩卡testing

问题似乎很微不足道,所以我真的很尴尬,我不能自己解决它,但我做了很多search谷歌和stackoverflow,没有任何帮助我呢。 我正在用node.js开发RESTful服务器,我想testing它的API。 服务器asynchronous初始化(它需要连接到数据库,从另一台服务器等请求令牌)。 所以我的server.js文件中的代码如下所示: var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient; var bunyan = require('bunyan'); // logger var routes = require('./routes/folrouter.js'); var TokenGetter = require('./utils/tokengetter'); MongoClient.connect('mongodb://localhost:27017/fol', function (err, db) { if (err) throw err; global.db = db; global.log = bunyan.createLogger({ name : "folserver", src : true, […]

在nodeclipse中是否有任何unit testing集成?

我正确地认为nodeclipse还没有提供unit testing集成到JUnit吗? 我希望能够单独运行testing,通过脚本,文件夹…但似乎无法find一种方法来做到这一点。 埃里克

Sails.js和Mocha:使用supertest创build一个新的模型

我目前正在为我的Sails应用程序设置testing基础架构,并且一直在顺利进行,直到我尝试使用supertesttesting来testingAPI请求。 我试图testing一些我的控制器方法(我实现,而不是使用默认蓝图路线),但似乎API请求甚至没有经过。 我认为这是因为我可以运行npm test ,这段代码运行良好,但是如果我将POSTpath更改为/datamodel/create5 ,其中create5()不作为控制器方法存在,它仍然运行正常。在这两种情况下,都不会创buildDataModel模型。 我已经在下面包含了一些代码。 这是我的代码看起来像: var request = require('supertest'); var assert = require('assert'); var async = require('async'); var stubs = require('../stubs.js'); describe('DataModel', function() { var testDataModel; var dataModelParams = stubs.dataModelStub(); // simply returns a JSON dictionary describe('#create()', function() { describe('data model import', function() { it('should import a new data model.', function (done) […]

运行并将unit testing添加到nodejs项目

我想在github上运行与这个项目相关的testing,请参阅https://github.com/node-opcua/node-opcua上的文件夹testing。 不幸的是,我不知道使用了哪个testing框架以及如何运行所有的testing?

如何在Node.js中模拟一个glob调用

我正在使用Mocha,Chai和Sinon JS为我的Node.js应用程序编写unit testing。 这是我想testing的模块: var glob = require('glob'); var pluginInstaller = require('./pluginInstaller'); module.exports = function(app, callback) { 'use strict'; if (!app) { return callback(new Error('App is not defined')); } glob('./plugins/**/plugin.js', function(err, files) { if (err) { return callback(err); } pluginInstaller(files, callback, app); }); }; 我有一个testing的情况下,当没有应用程序使用.to.throw(Error) 。 但是我不知道如何模拟glob调用。 特别是我想告诉我的testing方法,glob-call返回什么,然后检查,如果pluginInstaller被调用,使用sinon.spy 。 这是我迄今为止的testing: var expect = require('chai').expect, pluginLoader […]