Tag: 摩卡

摩卡beforeEach在一套房正在运行另一套房

我有两个testing套件(我使用摩卡的TDD UI,它使用suite() ,test()而不是describe()和it() ): suite('first suite'), function(){ …. }) suite('second suite', function(){ beforeEach(function(done){ console.log('I SHOULD NOT BE RUN') this.timeout(5 * 1000); deleteTestAccount(ordering, function(err){ done(err) }) }) … }() 运行mocha -g 'first suite只运行mocha -g 'first suitetesting,但运行beforeEach,打印I SHOULD NOT BE RUN在控制台上I SHOULD NOT BE RUN 。 我怎样才能使beforeEach()只运行在它所包含的套件? 注意 :我可以解决这个问题: beforeEach(function(done){ this.timeout(5 * 1000); if ( this.currentTest.fullTitle().includes('second suite') […]

Nodejs类与variables

我有这个parsing器类在一个文件中: module.exports = function Parser() { this.myVar = ""; var parse = function (inputString) { this.myVar = "somethingAfterParse"; } return { initWithString: function(inputString) { parse(inputString) }, name: this.myVar }; }; 然后,在“main”文件js中,我使用这样的parsing器: var Parser = require("./Parser.js"); var parserInstance = new Parser(); parserInstance.initWithString("somestring"); console.log("Parser var:", parserInstance.myVar); 没有错误,但日志打印一个空的名字..为什么?

使用webpack的require.ensure函数为javascript模块编写testing

我在我的服务器上运行摩卡testing,testing源脚本是一种单独的unit testing方式。 我正在testing的一个脚本调用了Webpack的require.ensure函数,这个函数在被Webpack绑定的时候在应用程序中创build代码分割点是非常有用的。 我为这个脚本编写的testing不在Webpack上下文中运行,因此require.ensure函数不存在,testing失败。 我试图为这个函数创build一些polyfill / stub / mock / spy,但是没有任何运气。 有一个包, webpack-require ,它允许创build一个webpack上下文。 这可以工作,但速度慢得令人难以接受。 我宁愿有一些直接针对require.ensure函数的轻量级require.ensure 。 任何build议? 🙂 这是一个非常基本的摩卡testing起点。 摩卡testing加载一个包含方法的人工模块,如果require.ensure被定义,则返回true。 foo.js export default { requireEnsureExists: () => { return typeof require.ensure === 'function'; } }; foo.test.js import { expect } from 'chai'; describe('When requiring "foo"', () => { let foo; before(() => { foo […]

严格的平等是否保证了平等

我开始用Mocha为我的nodeJS / Javascript应用程序编写unit testing。 对于我的平等主张testing,我决定使用 assert.strictEqualtesting相等 assert.notEqualtesting差异 这背后的想法是强制使用严格的相等运算符( ===和!== ),但要确保如果使用==和!=则不会产生错误。 但是这引起了我一个问题: 对于每一个可能的a做a === b意味着a == b并且做a !== b意味着a != b ?

MochaJS:如何在testing之间共享断言/“它('应该')”代码

我有一些使用NodejstestingWeb服务器的摩卡testing。 许多testing应该会导致服务器返回错误,例如400错误请求。 目前的testing是以下代码的许多副本: it('should respond with 400 (Bad Request)', function (){ expect(httpResponse.statusCode).to.equal(httpstatus.BAD_REQUEST); }); 这是一个简化的伪代码示例: describe('When passing bad JSON data', function(){ var response before(function(done){ callUrlToInsert(url, badJson, function(err, resp){ response = resp done() } } it('should respond with 400 (Bad Request)', function (){ expect(httpResponse.statusCode).to.equal(httpstatus.BAD_REQUEST) }) } 这使我感到困惑,因为作为一名程序员,我尽可能避免了重复的代码。 但是,把它放到一个函数中是行不通的。 function verifyItReturnedBadRequest400(httpResponse) { it('should respond with 400 (Bad Request)', […]

摩卡范围function未定义

我目前正在浏览一些使用Javascript的TTD教程,并且有一个似乎是Javascript而不是TTD问题的东西。 即,我有以下testing: 'use strict'; var chai = require('chai'); var expect = chai.expect; var sinon = require('sinon'); var orderSystemWith = require('../lib/orders'); //describe is used to display features //context is used to display scenarios //it is used to describe tests within a feature/scenario describe('Customer displays order', function () { beforeEach(function(){ this.orderDAO = { byId: sinon.stub() }; this.orderSystem […]

确保服务器应用程序在摩卡testing开始之前运行

这与在每次摩卡testing之前确保快速应用程序运行相似,但指定的解决scheme仍然无法正常工作+我正在使用websocket服务器 总之,我使用一个名为socketcluster的websocket框架,这是我的服务器文件 import {SocketCluster} from 'socketcluster'; const socketCluster = new SocketCluster({ workers:1, brokers:1, port: 3000, appName:null, initController: __dirname + '/init.js', workerController: __dirname + '/worker.js', brokerController: __dirname + '/broker.js', socketChannelLimit: 1000, crashWorkerOnError: true }) export default socketCluster 运行node server.js启动worker.js中指定的工作进程 export const run = (worker) => { console.log(' >> worker PID: ',process.pid); const app = express(); const […]

如何在node.js的正常closures过程中编写testing来检查行为?

有一个我的优雅关机的例子: function stopHandler() { logger.info(`Stopping server on port ${settings.port} with pid ${process.pid} started`); server.close(() => { logger.info(`Server on port ${settings.port} with pid ${process.pid} stopped`); process.exit(); }); setTimeout(() => { logger.info(`Server on port ${settings.port} with pid ${process.pid} stop forcefully`); process.exit(); }, settings.stopTimeout); }; process.on("SIGTERM", stopHandler); process.on("SIGINT", stopHandler); 我怎样才能用摩卡testing这个代码?

期望断言types错误 – >期望(…).toExist不是一个函数

我正在testing一个nodejs应用程序。 当我运行testing时发现这个错误。 testing脚本如下: .expect( (res) => { expect(res.headers['x-auth']).toExist(); expect(res.body._id).toExist(); expect(res.body.email).toBe(email); }) 错误显示: TypeError: expect(…).toExist is not a function 我该如何解决这个问题? TIA。

MakeFile使用NPM运行Mochatesting

我正在尝试创build一个MakeFile来运行与NPM的摩卡unit testing。 所以我安装了摩卡,并在下面创build了一个unit testing: {} project_root /test/test.js 现在,当我尝试“做testing”与答复: make:“testing”没有什么可做的。 这是我的MakeFile: test: @./node_modules/.bin/mocha -u tdd .PHONY: test 所以真正的基本。 我读过摩卡将自动运行“testing”目录中的所有testing。 我的MakeFile语法不正确吗? 谢谢!