Tag: unit testing

在POST请求中testing和存根参数

伙计们如何在POST请求中存储参数,例如这里是函数的一部分 gridCalculator : function(req,res){ // calculation logic var options=[]; options.dateFirstLicensed = req.param('DateFirstLicensed'); options.dateFirstInsured = req.param('DateFirstInsured'); options.claimList = req.param('ClaimList'); options.suspenList = req.param('SuspenList'); …etc 如果我这样做 it('grid Calculate', function (done) { var req = { 'DateFirstLicensed' : "01-01-2010", 'DateFirstInsured': "01-01-2011", 'ClaimList': ['05-03-2012'], 'SuspenList': [{'StartDate':'05-03-2012','EndDate':'05-05-2012' }] }; gridCalculator.gridCalculator(req,function (err, result) { result.should.exist; done(); }); }); 我得到错误,因为我只是传递一个对象而不是POST请求 TypeError: req.param is […]

从图书馆摩卡单位testingerror handling

我有下面的代码,基本上从数据库中获取用户。 下面的代码是使用Dynamoose,但我假设在许多其他库和包中有类似的东西。 User.get(searchid, function(err, user) { if (err) { console.log(err); return res.send('error'); } else { //DO LOGIC } }); 我写了unit testing来处理上面else语句中的所有逻辑。 但我想弄清楚是否有办法编写unit testing来处理代码的if (err)部分。 基本上我想写unit testing,以确保如果有错误,它会返回正确的东西,我会为任何其他testing我应该写的build议。 如何在这种情况下编写testing,并用unit testing来覆盖这些代码?

为事件创buildunit testing

我正在使用下面的开源解压文件正在工作正常。 现在我想创build一些集成testing ,看到文件被保存,问题是这个过程是事件的抛出,我不知道当过程结束时,任何想法如何解决这个问题? 前 var DecompressZip = require('decompress-zip'); var unzipper = new DecompressZip(filename) unzipper.on('error', function (err) { console.log('Caught an error'); }); unzipper.on('extract', function (log) { console.log('Finished extracting'); }); unzipper.on('progress', function (fileIndex, fileCount) { console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount); }); unzipper.extract({ path: 'some/path', filter: function (file) { return […]

如何模拟readline.on('SIGINT')?

我有这段代码: function getMsg() { return new Promise(function (resolve, reject) { var input = []; var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (cmd) { if (cmd.trim()) { input.push(cmd); } else { rl.close(); } }); rl.on('close', function () { rl.close(); resolve(input.join('\n')); }); rl.on('SIGINT', reject); }); } 我试图testing这个function,到目前为止,我的尝试是这样的: it('should reject if SIGINT is sent', […]

即使抛出错误,Jasmine toThrow也不起作用

即使发生错误,此代码也会失败。 it('should throw error when foo config value is falsey', (done) => { const config = { foo: null, bar: 'some-name' }; expect(quux.withConfig(config).load('*', (err, inst) => { done(); })).toThrow(); }); 我也试过这个 it('should throw error when foo config value is falsey', (done) => { const config = { foo: null, bar: 'some-name' }; expect(SelfServiceCompletedJobStore.withConfig(config).load('*', (err, inst) […]

JavaScript和Mocha的unit testing

我已经编写了一个JavaScript库/模块,用于调用静态Web服务的API调用,并向该库的使用者提供方法。 该库的构build方式使得相同的代码既可以用作前端库,也可以用作npm模块(使用browserify转换后的代码)。 不过,我现在计划编写相同的unit testing,这是我第一次编写整个库。 所以,我曾经看过像摩卡和茉莉花这样的图书馆,看来这是很好的select。 我正在考虑在摩卡书写testing。 但是,我不太清楚如何开始这些testing。 另外,像样本消费者那样testingfunction的testing应该被称为集成testing,还是应该将这些testing与unit testing结合起来? 我会很高兴,如果任何人都可以推荐一些在Github的样本testing或摩卡初学者的某种教程。 我也很乐意考虑其他的unit testing框架。 提前致谢

Sinon嘲笑Sequelize

我有以下使用Sequelize的nodejs函数: var processDatabase = function (dbConnection, schema, recordsets) { var myLogTable = dbConnection.define(schema.tableName, schema.myLogSchema, schema.myLogSchemaIndex); myLogTable.sync({ force: false, freezeTableName: true, logging: console.log }).then(function () { console.log('Table synced…'); for (k = 0; k < recordsets.length; k++) { var query = "Some query"; dbConnection.query( query, { type: dbConnection.QueryTypes.SELECT } ) .then(function (results) { console.log('MYSQL Selection Done'); }) […]

如何在节点的FileSystem.readfile中unit testing逻辑cb

目前,我有一个读取文件的function。 当我抛出并testingreadfilecallback以外的错误时,它将起作用: var doWork = function(path) { //throw new RangeError('blah'); // works, error is thrown fs.readFile(path, 'utf-8', function(error, data) { //etc…. logic.. etc.. if(data.split('\n')[0] > x) throw new RangeError('blah'); //does not work }); } 我的testing: describe('my test suite', function(){ it('should throw an error', function(){ var func = function() { doWork('my path'); } var err = […]

节点茉莉花没有失败时,预期

我正在尝试第一次设置node-jasminetesting。 目前我只是试图设置一个简单的testing,看到获得索引返回状态200。 它似乎工作,但我注意到,无论我改变状态号码,它永远不会失败,例如期待666状态,但我不会失败: const request = require("request") const helloWorld = require("../app.js") const base_url = "http://localhost:3002/" describe("Return the index page", function() { describe("GET /", function() { it("returns status code 200", function() { request.get(base_url, function(error, response, body) { expect(response.statusCode).toBe(666) done() }) }) }) }) 哪个返回: Finished in 0.009 seconds 1 test, 0 assertions, 0 failures, 0 skipped […]

在运行时和unit testing问题上需要文件

我使用require.js在运行时加载文件,如下所示:当我在正确的上下文中运行文件(我的意思是来自正确的path的调用)时, 按预期工作 。 module1.js define(["otherModule"], function(otherModule) { working!!! …. 现在我想创build一个unit testing从这个文件(模块1)从其他上下文(从在不同的位置在项目中find的testing文件夹),我得到错误 require.js:145 Uncaught Error: Script error for: otherModule 由于它试图在位于不同的项目结构中的unit testing期间在此path上运行get … HTTPS://app/path1/path2/path3/otherModule.js 而在运行时 (从不同的上下文),它在path中find它 HTTPS://app/path1/path2/path3/path4/path5/otherModule.js 请求中有额外的path4和path5,我应该如何解决它在两种情况下工作(UT / Runtime)? http://requirejs.org