Tag: chai

你将如何嘲笑/分支一个快速路线的请求?

例如,如果我想在这个给定的方法中嘲笑一个请求,我该怎么做? app.get('/test', exposeDb, function(req, res) { req.dbService.getUserByID(req.body, function(err, result) { if (result == 5) { console.log(result); res.json({result: result}); } else { console.log(result); res.json({result: result}); } }); }); 这是我目前的testing: describe('Test', function() { beforeEach(function() { var getUserByIDStubE = sinon.stub(dbService, 'getUserByID').callsFake(cb => { cb(undefined, 5); }); }); it('TESTSETSTETS', function(done) { chai.request(server) .get('/test') .send({ req }) .end(function (err, res) […]

摩卡和柴:写作断言

我正在使用摩卡和柴第一次写testing用例。 这是我的服务返回的非常简单的响应: { "success": true, "message": "Done" } 我写过这样的断言: describe('GET /TestService', function() { it('should run successfully.', function(done) { request .get('/myApp/TestService/') .expect(200) .expect('Content-Type', 'application/json') .end(function(err, res) { expect(res.body.success).to.equal(true); expect(res.body.message).to.equal('Done'); }); }); }); 当我执行testing时,它返回: Uncaught AssertionError:预计未定义为等于true 这有什么问题? 这是我第一次遇到摩卡和柴。 也许我错过了一个非常基本的东西。 这只是第一步。 有人能推我吗?

NodeJS模仿保存模型似乎不工作

试着unit testing我的小api,我需要testing保存错误和成功(已经注册等)。 目前这里是我的代码: models / auth.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var AuthSchema = Schema({ id: String, user: String, password: String, origin: String, }); //Export model module.exports = mongoose.model('user', AuthSchema); testing/ api.test.js const chai = require('chai'); const should = chai.should; const expect = chai.expect; const assert = chai.assert; / Node Http Mocks […]

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

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

ChaiJS不处理exception

这是一个NodeJS模块: var plural = function(words, num) { if (words.length != 3) { throw new Error('Wrong words array'); } var lastNum = num % 10; var second = (num / 10) % 10; if (second == 1) return words[2] else if (lastNum == 1) return words[0]; else if (lastNum <= 4) return words[1]; else return words[2]; […]

我可以从testing中连接mongoose吗?

我正在尝试使用mongoose运行连接到mongodb的chaitesting,但是由于“预期未定义为对象”而失败。 我正在使用function相同的方法。 我是否正确连接到数据库? var expect = require('chai').expect; var eeg = require('../eegFunctions'); var chai = require("chai"); var chaiAsPromised = require("chai-as-promised"); chai.use(chaiAsPromised); var mongoose = require('mongoose'); var db = mongoose.connection; db.on('error', console.error); db.once('open', function callback(){console.log('db ready');}); mongoose.connect('mongodb://localhost/eegControl'); test("lastShot() should return an object", function(){ var data; eeg.lastShot(function(eegData){ data = eegData; }); return expect(data).to.eventually.be.an('object'); });

使用Mocha和Chai编写testing用例

我有一个简单的函数: var moment = require('moment-timezone'); exports.splitIntoDays = function(from,to) { var timeIntervals = []; var interval = {}; var start = moment(from); var end = moment(to); if(start.isAfter(end)) { throw new Error('From date ('+from+') is after To date ('+to+').Enter a valid date range.'); } var initial = start; console.log("Before loop"+initial.format("YYYY/MM/DD-HH:mm:ss")+" "+initial.diff(end,'hours')); while(end.diff(initial,'hours') > 24) { timeIntervals.push({"from" : […]

允许chai / mochatesting将错误冒泡到process.on

我正在编写一个捕获顶级未捕获错误的节点模块,并希望为其编写一些testing。 不幸的是,我最喜欢的框架似乎有意识地抛出和捕获未捕获的exception的问题。 如果我抛出exception,那么错误,然后testing失败。 如果我抛出并捕获错误,它不会冒泡process.on('uncaughtException') 代码atm不起作用 it('Catches errors and return the user and line number', function(done) { blame.init(function (res) { console.log('\n\n', res, '\n\n'); expect(true).should.equal(true); done(); }); expect(function () { undefinedFunction(); }).to.throw('undefinedFunction is not defined'); });

使用mocha.js在node.js中testing外部API调用

我正在尝试为我的npm模块编写testing,该模块负责与后端api进行通信。 这个模块将坐在一个cordova的Android应用程序,并会照顾任何API调用。 我所遇到的问题似乎是对摩卡的理解,但是我已经在互联网上看了一遍,也找不到解决办法,所以我转向群众。 作为一个例子,我有一个像这样的function test: function() { request.get({ url: defaultHost, headers: { } }, function(err, httpResponse, body) { if(err) { return err; } else { console.log(body); return body; } }); } 这个作品会的。 我现在正在尝试在摩卡中为它创buildtesting。 我得到的问题是,我不知道如何从.get调用返回到摩卡testing函数。 api返回json,所以我知道我将不得不做一个平等的比较,但是目前我甚至不能打印结果。 我认为问题在于,用我的其他摩卡testing,我可以工作,他们都有一个论点,你通过的地方,因为这不是。 我目前的testing代码是这样的。 describe('#test', function() { it('tests api reachability;', function() { var test = new test(); }); }); 如果有人可以解释什么是必要的,甚至只是指向我在谷歌的正确方向,这将是很棒的。 我通常在谷歌相当好,但没有find方向在这一个。

我如何使用Mocha和伊斯坦布尔编写出色的testing用例?

我正在尝试使用chai和Mocha为Mongoose模型函数编写unit testing用例。 模型function function getDetails(parameter, fn) { Model.findOne({ parameter: parameter }) .lean() .exec(function(err, document) { if (err) { return fn(err, null); } return fn(err, document); }); }; unit testing用例 describe('→ Database model functions.', function() { it('getDetails() – should fetch and return details from database', function(done) { model.getDetails(parameter, function(err, document) { expect(err).to.be.null; expect(document).not.to.be.null; expect(document).to.be.an('object'); done(); }); […]