如何使用chai和mochatestingsubprocess?

我正在创build一个框架来在特定的时间执行进程(cron-like),并testing我正在使用chai-mocha-grunt。

解决scheme的体系结构基于此示例 。 基本上,我们有:

  • 一个主进程,通过特定的次数调用subprocess(通过child_process.fork)。
  • 一个subprocess,它使用setInverval()来执行某些事情;
  • 调用Master.start()函数的过程。

有了这个架构,我该如何testing,以确保线程在正确的时间使用摩卡和柴(与“断言”库)执行?

换句话说,我该如何让线程“听”线程并检查它们是否在正确的时间执行?

我不确定你需要chai本身来听你的线程。 如果你正在构build你关联的例子,那么这个应该是非常简单的,因为Master.js已经是一个EventEmitter了,它已经发出了它从subprocess中听到的所有事件。

你的testing结构可以像这样简单:

 describe('ForkExample test', function() { // Set an appropriate test timeout here this.timeout(1000); it('should do stuff at the right time', function(done) { var fe = new ForkExample(); fe.start(1); fe.on('event', function(type, pid, e) { if (type === 'child message') { // Check here that the timing was within some expected range done(); } }); }); });