Tag: 摩卡

补丁/模拟函数内的另一个函数在JavaScript中进行testing

基本上我想打补丁函数调用另一个函数,以便我可以确认他们已经被调用。 这在js中可能吗? (我正在使用Mocha / Chai / Sinon进行testing)。 我想要做的简化版本: // in render.js export helper = function() {} export default = function() { helper() } // in the render-test.js import render, { helper } from 'render' // what I'd like to to: helper = sinon.spy() render() assert(helper.called, true)

当用Chai进行unit testing时,“TypeError:无法读取属性”的“undefined”地址是什么意思?

当使用chai进行unit testing并且没有任何testing通过时,我仍然收到这个错误消息。 这在这方面意味着什么? 谢谢。 var chai = require('chai'); var chaiHttp = require('chai-http'); var server = require('../server.js'); var should = chai.should(); var app = server.app; var storage = server.storage; chai.use(chaiHttp); describe('Shopping List', function() { it('should list items on GET', function(done) { chai.request(app) .get('/items') .end(function(err, res) { res.should.have.status(200); res.should.be.json; // jshint ignore:line res.body.should.be.a('array'); res.body.should.have.length(3); res.body[0].should.be.a('object'); res.body[0].should.have.property('id'); res.body[0].should.have.property('name'); […]

Promise.all()等同于promise.then()。then()

这是代码 Promise.all([ promise1, promise2, ]) .then(() => doSomething()) 相当于 promise1 .then(() => promise2) .then(() => doSomething()) 我认为它们是相同的,但是它们在财富和摩卡应用上performance不一样。 以下是关于这个应用程序的更多细节 我正在使用fortune.js ,我想用摩卡书写一些testing。 我想要实现的是使用beforeEach钩截断数据库中的表,然后插入一些preifined值。 所以,如果我有两个名为customer和user表,我会做这样的事情 beforeEach(function () { return Promise.all([store.delete('user'), store.delete('customer')]) .then(() => store.create('customer', { id: '0987654321234567890', // More data })) .then(() => store.create('user', { id: 'qwertyuioppoiuytrewq', customer: '0987654321234567890', // More data })); }); 这个代码不稳定,有时候工作,有时不工作,我可以find为什么(成功率约50%), 但是,如果我切换到这个代码它正在工作: beforeEach(function () […]

在摩卡testing中使用for循环

我是摩卡新手,我想尝试使用for循环来创buildtesting用例。 我想testing一个我input标准12小时时间的函数,并把它输出到24小时军事时间。 这是它的样子。 exports.main = function(time) { var hr = parseInt(time.substr(0,2)); var period = time.substr(8,10); if (period == 'AM' && hr == 12) { hr = '0'; } if (period == 'PM' && hr < 12) { hr += 12; } hr.toString(); if (hr < 10) { hr = '0' + hr; } return time […]

asynchronous摩卡testing(与Chai断言库)应该失败,但被标记为通过

我已经在TypeScript中使用mochatesting框架和chai断言库(我对这三者都是新手)写了一些testing,并看到引发了一个错误,并在下面的代码中调用了assert.fail()。 但是,testing仍然被标记为通过。 我很困惑,为什么会这样呢,如果我在承诺上做错了什么。 另外,如下所示,还有一个UnhandledPromiseRejectionWarning。 我不明白为什么这被标记为未处理,因为我已经包括一个catch块。 我将不胜感激关于如何解决这个问题的build议,谢谢! User.all() .then((users) => { expect(users.length).to.equal(0); return User.create('test@test.email', '12345', 'test', true, true); }) .then(() => { return User.all(); }) .then((users) => { expect(users.length).to.equal(1); }) .catch((error) => { assert.fail(); }); debugging控制台 ✓ should create a new record with existing e-mail in the database (node:29903) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): […]

在Mocha中asynchronoustesting大量数据

我需要通过电子邮件列表循环,并检查他们validation。 如果一个人不能确认,我会抛出一个错误让摩卡知道。 it('validates valid email addresses', function (done) { [ 'oliverash@me.com', 'oliver.ash@me.com', 'oliver-ash@me.com', 'oliver+ash@me.com', 'oliver\'s-email@me.com' ].forEach(function (value) { user.email = value user.validate(function (err) { // Failed to validate, throw an error if (err) { throw new Error() } }) }) // All documents validated, so call `done` done() }) 但是,这个代码似乎并不正确,只有数组中的最后一封电子邮件无效时才会失效。 我究竟做错了什么?

在摩卡testing中没有发现柴的出口

我已经创build了简单的摩卡testing。 当使用Node“assert”模块时,它完美地工作。 我从命令行(Mocha作为全局节点模块安装)运行它: $ mocha myTest.js ․ 1 test complete (6 ms) 该脚本如下所示: var assert = require("assert") describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ assert.equal(-1, [1,2,3].indexOf(5)); assert.equal(-1, [1,2,3].indexOf(0)); }) }) }) 那么,我试图添加柴而不是断言库。 我先安装它: npm install chai 所以,在我的项目中创build了目录node_modules。 太好了。 然后,我改变了脚本使用柴: var chai = require("chai"); describe('Array', function(){ describe('#indexOf()', function(){ it('should […]

mocha节点检查器不能使用“debugging器”命令

我正在使用摩卡来testing我的代码。 我正在使用节点检查器来debugging我的代码。 bash mocha test/test.* –debug-brk 这工作,但不是很好。 它停在摩卡的第一行代码。 我希望它停止在我的代码。 我尝试使用“debugging器”关键词做一个手动断点,但一些如何不停止在那里。

两个文件使用超级摩擦引起EADDRINUSE摩卡

我使用supertest来unit testing我的服务器configuration和路由处理程序。 服务器configurationtesting在test.server.js ,路由处理testing在test.routes.handlers.js 。 当我使用mocha .运行所有的testing文件mocha . ,我得到EADDRINUSE 。 当我单独运行每个文件时,一切都按预期工作。 这两个文件定义并要求supertest, request = require('supertest')和express服务器文件app = require('../server.js') 。 在server.js ,服务器是这样启动的: http.createServer(app).listen(app.get('port'), config.hostName, function () { console.log('Express server listening on port ' + app.get('port')); }); 我的实现有什么问题吗? 运行testing时如何避免EADDRINUSE错误?

如何在Webstorm上使用mocha运行unit testing?

我试图直接在WebStorm IDE上运行摩卡testing,但没有任何成功。 这是我的jsTestDriver.conf文件: server: http://localhost:4224 load: – node_modules/mocha/mocha.js – node_modules/chai/chai.js – node_modules/requirejs/require.js – mocha-jstd-adapter/src/MochaAdapter.js – routes/*.js test: – test/*.js timeout: 90 现在我正在看一下正常工作的buster-format模块。 任何帮助表示赞赏(如果我这样做,我会发布解决scheme)。