Tag: chai

使用柴和冼方法在一个方法内扼杀诺言

我testing的function大致是这样的; function doThing(data, callback) { externalService.post('send').request(data) .then(() => { if (callback) { callback(); } }) .catch((message) => { logger.warn('warning message'); if (callback) { callback(); } }); } 我试图用柴和诗乃来testing。 我尝试过不同的指南,我目前的咒语看起来像; const thingBeingTested = require('thing-being-tested'); const chai = require('chai'); const sinon = require('sinon'); require('sinon-as-promised'); const sinonChai = require('sinon-chai'); const expect = chai.expect; var chaiAsPromised = require('chai-as-promised'); chai.use(chaiAsPromised); […]

用chai发送一个POST请求发送一个空的正文?

我现在有以下设置 test.js var user = { username: 'test_user', email: 'test@test.me', password: 'you shall not pass', address: 'No where street' }; chai.request(app) .post('/api/v1/users') .send(user); 我正在处理我的routes / user.js中的发布请求 router.post('/', function(req, res, next) { console.log('body: ' + req.body); queries.insertUser(req.body) .then(function(id) { return queries.getSingleUser(id); }) .then(function(user) { res.status(200).json(user); }) .catch(function(err) { next(err); }); }); req.body最后是未定义的。 任何线索可能会出错? 如果有人想看看,代码是https://ide.c9.io/burtonium/node-from-scratch 。

使用mocha / chai来确保REST API提供一个文件?

我想validation一个调用我的REST API的端点之一是提供一个文件,但我不知道如何去做,我没有看到任何这方面的例子? 我看过文档 ,但是这对我没有多大帮助。 服务器端代码本质上是(在Express中): handleRetrieveContent(req, res, next) { const filepaht = '…'; res.sendFile(filepath) } 和testing案例: it('Should get a file', (done) => { chai.request(url) .get('/api/exercise/1?token=' + token) .end(function(err, res) { if (err) { done(err); } res.should.have.status(200); // Not sure what the test here should be? res.should.be.json; // TODO get access to saved file and do […]

testing总是与诗乃和柴一起传递

我正在使用Mocha,Chai和Sinon来testing一些Node方法。 这个testing通过,当我把'calledOnce'改为'calledTwice'时,它会按预期的那样失败。 it('should call checkIfRoomExists once', function (done) { var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists'); ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) { expect(check.calledOnce).to.equal(true); done(); }) }); 但是,当我尝试并按照教程“设定”是这样设置的: it('should call checkIfRoomExists once', function (done) { var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists'); ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) { expect(check).to.have.been.calledTwice; done(); }) }); 请注意,我在第二个testing中正在testing“calledTwice”。 它仍然通过。 如果我将它更改为“notCalled”,它仍然通过。 基本上它总是通过。 我错过了什么?

如何在节点的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 = […]

如何通过摩卡和柴匹配在网页上呈现的特定string

我正在使用mocha和chai包在我的Node JS应用程序中构build一个testing套件。 到目前为止,我通过检查页面成功呈现时返回的状态码(200)来validationtesting用例是否合格/不合格。 如何根据我在页面上呈现的内容来validation页面是否合格/不合格。 例如,如果页面显示“欢迎使用Express”,我想匹配“Welcome”或“Express”中的任何一个来validation它是否合格。 以下是我的代码检查状态代码的片段: describe('Home Page', () => { it('This is the Home page', (done)=> { chai.request(server) .get('/home') .end((error, respond) => { expect(respond.statusCode).to.equal(200); done(); }); }); });

Sinon js用确切的参数检查存根

Sinon js用确切的参数检查存根 要求:我想testing用正确的参数调用ejs.renderFile。 我的函数文件:html_to_pdf_converter.js var ejsToPdfConvert = function (template, data, callback) { var row = data.voucher; html = ejs.renderFile( path.join(__dirname+'/../../views/', template), { data: data }, function (error, success) { if (error) { callback(error, null); } else { var pdfPath = getPdfUploadPath(row); htmlToPdf.convertHTMLString(success, pdfPath, function (error, success) { if (error) { if (typeof callback === 'function') […]

unit testing模块在承诺解决时调用私有函数

我是Node的unit testing新手,在承诺方面遇到了障碍。 我的模块通过ApiController执行search,然后返回一个promise。 根据其parsing状态,它调用模块中的两个私有函数之一。 模块: module.exports.process = function(req, res) { let searchTerm = req.query.searchValue; ApiController.fetchResults(searchTerm) .then((results) => onReturnedResults(results), (err) => onNoResults()); function onReturnedResults(results) { req.app.set('searchResults', results); res.redirect('/results'); } function onNoResults() { res.render('search/search', { noResultsFound: true }); } }; testing: var res = { viewName: '', data : {}, render: function(view, viewData) { this.view = view; […]

使用我的Node.js REST API设置Mocha / ChaiHttp单元时遇到问题

我正在尝试使用mocha和chaihttp为我的node.js REST API设置unit testing,但是我应该通过的示例testing似乎失败了。 我的unit testing代码在这里: var express = require('express'); var router = express.Router(); var chai = require("chai"); var chaiHttp = require('chai-http'); chai.use(chaiHttp); describe("Test", function() { it('Simple test', function(done) { chai.request('http://localhost:3000/') .get('/') .end(function(err, res) { chai.expect(res).to.have.status(200); done(); }); }); }); 运行testing时出现的错误: 1) Test Simple test: Uncaught AssertionError: expected { Object (domain, _events, …) } to […]

Nodejs摩卡unit testingsqlite3callback未调用插入未执行

我想为我的应用程序创buildunit testing。 当通过浏览器调用API时,API工作得很好,但是当我想要执行testing时,无论是使用npm test还是mocha test,都不会调用callback函数,也不会执行INSERT语句。 我没有错误。 码: unit testing文件: process.env.NODE_ENV = 'test'; var chai = require('chai'); var chaiHttp = require('chai-http'); var server = require('../server/server'); var Const = require("../config/const.js"); var should = chai.should(); chai.use(chaiHttp); var assert = chai.assert; var column1value = "1234567890123456" describe('All', function() { it('Insert',function(done) { chai.request(server) .post('/' +Const.SERVER_NAME + '/insert') .set('content-type', 'application/x-www-form-urlencoded') .send({param1: column1value, […]