Tag: chai

使用nodejs,mocha,chai和sinon我想存根在路由中调用的函数

我有一个非常简单的应用程序,它是nodejs,express和mysql我是新的unit testing,我认为这个应用程序是一个很好的方法来做得更好。 我试图完成(我认为sinon是答案)是模拟或存根mysql.insertEventIntoDB和rabbit.addToRabbitMQ 在我的应用程序中,我有 app.use('/sendgrid', sendgrid(pool, config, logger) ); 在我的sendgrid.js中 var express = require('express'); var mysql = require('../app/utils/mysql'); var rabbit = require('../app/utils/rabbitMQ'); module.exports = function (dbpool, config, logger) { var router = express.Router(); router.post('/callback', function (req, res) { for (var x=0 ; x < req.body.length ; x++ ){ mysql.insertEventIntoDB(dbpool, req.body[x], logger); rabbit.addToRabbitMQ(config,req.body[x], logger) } res.json({ […]

Chai / Moka – > TypeError:request.get(…).expect不是一个函数

第一次与TDD。 在阅读了一些关于我的NodeJS API的文章之后,我使用了Chai / Moka二人组合。 我已经做了一些愚蠢的testing,学习如何使用这些。 现在我想testing我的API,所以我创build了一个路线: app.get('/hello', function(req, res) { res.status(200).send('Hello World!') }) 我尝试这样的testing: var request = require('superagent') var expect = require('Chai').expect […] describe('When request baseURL/hello', function(){ it('should salute you !', function (done) { request .get(baseURL + '/hello') .expect(200) .end(function(err, res){ if(err) return done(err) done() }) }) }) 我有失败输出: TypeError: request.get(…).expect is not a […]

unit testing与Mocha,Chai和Sinon的Node.js应用程序

我是unit testingNode.js应用程序的新手。 经过一些过滤,我的应用程序将CSV文件转换为JSON。 var fs = require('fs'); var readline = require('readline'); module.exports = ((year) => { if (typeof year !== "number" || isNaN(year)){ throw new Error("Not a number"); } var rlEmitter = readline.createInterface({ input: fs.createReadStream('./datasmall.csv'), output: fs.createWriteStream('./data.json') }); rlEmitter.on('line', function(line) { /*Filter CSV line by line*/ }); rlEmitter.on('close', function() { /*Write to JSON*/ }); }); […]

柴应该是不确定的

我已经安装了摩卡和柴。 在我的unit testing中: import {expect, should} from "chai"; describe("array", function () { it("has length of 1", function (done) { var arr = ["B"]; expect(arr).have.lengthOf(1); arr.should.have.lengthOf(1); }); }); expect按预期工作,但should是不确定的。 为什么?

chaiHttp忽略端口

我正在尝试使用chaiHttp编写testingnodejs-powered API。 这是我testing的代码: const mongoose = require("mongoose"); const Products = require('../models/product'); const chai = require('chai'); const chaiHttp = require('chai-http'); const server = require('../server'); const should = chai.should(); const assert = chai.assert; chai.use(chaiHttp); describe('Products', () => { beforeEach(done => {Products.remove({}, done);}); describe('/GET products', () => { it('it should GET all the products', (done) => { //chai.request(server) […]

为什么Mocha的`before()`函数传递是否带参数?

在我的describe有这样的代码: before(a) 。 当看起来像这样: function a() { return chai.request(app) … .then(res => { res.blah.should.blah; return Promise.resolve(); }); } …一切都很好(这是一个非常快的电话)。 但是,当我做ainputvariables: function a(dummy, my_var) { return chai.request(app) … // use my_var here .then(res => { res.blah.should.blah; console.log("finished!"); return Promise.resolve(); }); } 承诺永远不会解决。 两点意见: finished! 确实得到正确的输出(立即) dummy填充。 这是填充这个: function (err) { if (err instanceof Error || toString.call(err) […]

摩卡忽略了一些testing,尽pipe它们应该运行

我正在重构我的express-decorator NPM包的克隆。 这包括重构先前使用AVA进行的unit testing 。 我决定使用Mocha和Chai来重写它们,因为我喜欢他们定义testing的方式。 那么,我的问题是什么? 看看这个代码(我把它分解来说明问题): test('express', (t) => { @web.basePath('/test') class Test { @web.get('/foo/:id') foo(request, response) { /* The test in question. */ t.is(parseInt(request.params.id), 5); response.send(); } } let app = express(); let controller = new Test(); web.register(app, controller); t.plan(1); return supertest(app) .get('/test/foo/5') .expect(200); }); 此代码工作 。 这里(基本上)是相同的代码,现在使用Mocha和Chai和多个testing : describe('The test express […]

摩卡与期望是不工作的testing错误

在下面的脚本中只有一个testing通过。 testing错误(throw Error())失败,消息1)testing应该抛出错误: var expect = require('chai').expect; describe("a test", function() { var fn; before(function() { fn = function(arg){ if(arg == 'error'){ throw new Error(); }else{ return 'hi'; } } }); it("should throw error", function() { expect(fn('error')).to.throw(Error); }); it("should return hi", function() { expect(fn('hi')).to.equal('hi'); }); }); 如何改变期望来testing错误?

如何使用Chai Http发布对象数组

我试图用ChaiHttp这样发布一个对象数组: agent.post('route/to/api') .send( locations: [{lat: lat1, lon: lon1}, {lat: lat2, lon: lon2}]) .end (err, res) -> console.log err, res 它返回一个错误,如下所示: TypeError: first argument must be a string or Buffer at ClientRequest.OutgoingMessage.end (_http_outgoing.js:524:11) at Test.Request.end (node_modules/superagent/lib/node/index.js:1020:9) at node_modules/chai-http/lib/request.js:251:12 at Test.then (node_modules/chai-http/lib/request.js:250:21) events.js:141扔呃; //未处理“错误”事件^ 错误:在Zlib._handle.onerror错误的标题检查(zlib.js:363:17) 我也试图像这样发布信息,就像我们对邮递员所做的一样: agent.post('route/to/api') .field( 'locations[0].lat', xxx) .field( 'locations[0].lan', xxx) .field( 'locations[1].lat', xxx) .field( […]

如何使用Mocha和ChaitestingLogin API NodeJs

我是新来的testing驱动开发,我想testing我的loginAPI,但我似乎无法完全理解如何实现与数据库的testing,什么是正确的方法来做到这一点?