Tag: unit testing

茉莉花节点testing孩子process.send

我有一个node.js应用程序,它有一个主进程main-process.js和一个subprocesschild-process.js 。 main-process.js看起来像这样, var childProcess = require('child_process'); var job = childProcess.spawn('node', ["child-process.js"], { detached = true, stdio: ['ipc'] }); 我的child-process.js执行一些任务,并通知父进程有关它的状态,它使用 exports.init = function() { //some processing here process.send({status: 'success or pending'}); } 现在我想用jasmine-node对unit testingchild-process.js。 但是当我从spec中调用init()方法时,jasmine-node会抛出一个错误 TypeError: Object #<process> has no method 'send' 有什么办法模拟processvariables? 换句话说,我该如何testing这种情况?

用mongoose抽象类

我正在开发一个我需要抽象的应用程序。 我的意思是说,我想“模拟”一个界面行为,比如在我的具体类中创build一个契约。 其实,处理用户,我有一个UserMongoRepository类与合同执行: getAll()通过promise返回用户的完整列表 getById(id)返回promise所关心的用户 保存(用户)保存用户的承诺 …等 我在UserMysqlRepository内部实现了相同的方法(当需要更改时允许我切换行为。 问题 我的问题是,我正在处理的Mongoose不像数据映射器,但更像是一个活跃的logging。 这意味着我的保存(用户)的实现将有点奇怪,如下所示: save(user){ let mongooseUser = this.convert(user); return user.save(); } convert方法允许我从标准模型切换到特定的Mongoose模型。 它允许我再次抽象一些,不必重写我的完整的应用程序数据访问。 我真正的问题是当我尝试unit testing我的全class时: import MongooseUser from '../../auth/mongooseModel/MongooseUser'; /** * UserMongoRepositoryclass */ export default class UserMongoRepository{ /** * Create an UserMongoRepository */ constructor(){ } /** * Convert a User to a MongooseUser */ convert(user){ return new […]

如何多次声明stubbed的提取

使用proxyquire,sinon和mocha。 我可以在第一次调用fetch时存根。 但是在recursion调用的第二次调用中,我不能断言它。 从输出结果看,在testing结束之前,断言可能会运行。 在断言之后,您将会看到second fetch控制台。 index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=> { console.log('error') }); } function b() { fetch('https://www.google.com') .then((e) => { console.log('second fetch'); }) .catch((e)=> { console.log('error') }); } a() testing: describe('fetch test demo', ()=> { it('fetch should of […]

NodeJS – 在process.oncallback中testingunit testingsetTimeout

我正在尝试使用Jest在我的process.on('SIGTERM')callback中testing一个计时器,但是它似乎永远不会被调用。 我正在使用jest.useFakeTimers() ,虽然它似乎将setTimeout调用模拟到一个范围,但它不会在检查setTimeout.mock对象时结束。 我的index.js文件: process.on('SIGTERM', () => { console.log('Got SIGTERM'); setTimeout(() => { console.log('Timer was run'); }, 300); }); setTimeout(() => { console.log('Timer 2 was run'); }, 30000); 和testing文件: describe('Test process SIGTERM handler', () => { test.only('runs timeout', () => { jest.useFakeTimers(); process.exit = jest.fn(); require('./index.js'); process.kill(process.pid, 'SIGTERM'); jest.runAllTimers(); expect(setTimeout.mock.calls.length).toBe(2); }); }); 并且testing失败: 预期值为(使用===):2收到:1,控制台日志输出为: console.log […]

所有的testing都运行完毕之后再进行清理

是否有可能在Jest运行完成所有其他testing后运行的清理或拆卸任务? 与setupFiles允许在运行任何testing之前设置任务类似。 如果这也可以运行,不pipetesting是否有任何错误,奖励点。 将afterAll(() => {})置于文件顶层(在任何describe函数之外)时,只会在完成特定文件的testing后才运行。 这个用例是我有许多testing文件将在一个开发数据库中创build用户,我不想让每个testing文件负责清理和删除用户。 编写testing时也可能发生错误,所以如果清理发生而不pipe错误是否会更好。

unit testing事件驱动的JavaScript

我在用node.js编写一个服务器端应用程序,并希望包含unit testing。 我正在努力的一件事是unit testingEventEmitter和setInterval/setTimout的好方法 我有什么selectunit testingasynchronous服务器端JavaScript? 我知道我可以将另一个侦听器附加到作为testingfunction的EventEmitter,但是我怎样才能让testing函数运行? 部分unit testing是为了确保unit testing的每个部分都运行。 我可以自己使用setTimeout,但这似乎是一个黑客的解决scheme。 如果这有帮助,那么我试图testing一些exampler代码。 … function init(param) { … // run update entities periodically setInterval(this._updateEntities.bind(this, param.containerFull), 1000 / param.fps); … } … EntityUpdater.prototype = { … "_updateEntities": function updateEntitiesfunc(fn) { this._tickEmitter.emit( "tick", new dataContainer.DataContainer(this.getEntityCount()) .on( "full", fn.bind(this) ) ); }, … } … (发射会触发一个事件) [编辑] 我开始阅读一些EvevntEmittertestinghttps://github.com/ry/node/tree/master/test/simple ,这有助于我看到如何去做这件事。

在node.js中testing响应?

我刚刚下载了mocha.js并用expect.js运行了一些基本的testing,以确保它正常工作。 但是在我的node应用程序中testing特定url的响应呢? 也就是说,我如何testing我从导航到/users得到什么答案? 使用mocha.js的前身Expresso,我可以做assert.response(server, req, res|fn[, msg|fn])并testing响应。

让Jasmine在服务器端运行

我正在尝试开始使用Node.JS在服务器端testingJavaScript 我正在使用grunt和grunt-jasmine-node ,但我无法运行我的testing。 它只是logging PS C:\ Development \ NET \ Source \ jsn> grunt运行“jasmine_node”任务未定义 在0.001秒内完成0个testing,0个断言,0个失败 完成,没有错误。 这是我的Gruntfile.js module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), jasmine_node: { specNameMatcher: 'spec', projectRoot: '.', requirejs: false, forceExit: true, jUnit: { report: false, savePath : "./build/reports/jasmine/", useDotNotation: true, consolidate: true } } }); grunt.loadNpmTasks('grunt-jasmine-node'); grunt.registerTask('default', 'jasmine_node'); }; 而我的文件结构是这样的: 那么,我做错了什么? 这就像stub.js文件甚至没有加载,因为它有一个console.log调用来testing。

使用Mocha&Should.js组织testing

我是使用Mocha&should.js进行unit testing的新手。 我正在使用Mocha的BDD来testing我的应用程序。 我正在testing的应用程序有不同的组件,如帐户,产品和订单。 在将代码移动到git仓库之前,我想testing应用程序的所有方面。 我对所有组件都有不同的testing文件。 例如,账户的account.js,订单的order.js等等。 我想testing一个临时testing帐户的所有组件。 所以stream程是: 创buildtesting帐户 testing与帐户相关的所有function(更新configuration文件,更改密码等) testing帐户产品的所有function testing账户订单的所有function 删除testing帐户及与其相关的所有信息 我的问题是如何确保在执行其他testing之前创build临时帐户? 因为我在不同的文件中testing用例,所以我如何确保它们的执行顺序与上面提到的相同? 还有其他更好的方法来testing应用程序吗? 谢谢。

存根Date.now()和Math.random()

我正在用Sinon使用Mocha来testing我的node.js模块。 我已经成功地嘲笑了其他的依赖(我写的其他模块),但是我遇到了困扰非纯函数(如Math.random()和Date.now() )的问题。 我已经尝试了以下(简化,以便这个问题不是如此本地化),但Math.random()不存在,因为一个明显的范围问题。 Math的实例在testing文件和mymodule.js之间是独立的。 test.js var sinon = require('sinon'), mymodule = require('./mymodule.js'), other = require('./other.js'); describe('MyModule', function() { describe('funcThatDependsOnRandom', function() { it('should call other.otherFunc with a random num when no num provided', function() { sinon.mock(other).expects('otherFunc').withArgs(0.5).once(); sinon.stub(Math, 'random').returns(0.5); funcThatDependsOnRandom(); // called with no args, so should call // other.otherFunc with random num other.verify(); // ensure […]