摩卡监控应用输出

我正在为nodejs web应用程序构build一个日志logging模块。 我想能够使用mochatesting,我的模块输出正确的消息到terminal 。 我一直在环顾四周,但还没有find任何明显的解决scheme来检查这一点。 我已经发现

 process.stdout.on('data', function (){}) 

但一直没有能够得到这个工作。 有没有人有任何build议?

process.stdout永远不会发出'data'事件,因为它不是一个可读的stream。 如果您好奇的话,可以阅读节点stream文档中的所有内容。

据我所知,挂接或捕获process.stdoutprocess.stderr最简单的方法是用一个你想要的函数replaceprocess.stdout.write 。 超级哈克,我知道,但在testing的情况下,你可以使用钩子之前和之后,以确保它被解开,所以它或多或less是无害的。 因为无论如何它都会写入底层的stream,如果你还没有解开它,那么它不是世界末日。

 function captureStream(stream){ var oldWrite = stream.write; var buf = ''; stream.write = function(chunk, encoding, callback){ buf += chunk.toString(); // chunk is a String or Buffer oldWrite.apply(stream, arguments); } return { unhook: function unhook(){ stream.write = oldWrite; }, captured: function(){ return buf; } }; } 

你可以在这样的摩卡testing中使用它:

 describe('console.log', function(){ var hook; beforeEach(function(){ hook = captureStream(process.stdout); }); afterEach(function(){ hook.unhook(); }); it('prints the argument', function(){ console.log('hi'); assert.equal(hook.captured(),'hi\n'); }); }); 

这里是一个警告:摩卡记者打印到标准输出。 就我所知,他们并没有这样做,而(例如( it('...',function(){}) )函数正在运行,但是如果您的示例函数是asynchronous的,则可能会遇到麻烦。 我会看看能不能find更多的这个。

我试图jjm的答案,并有问题,我怀疑是由于我的程序asynchronous行为。

我在github上find了一个使用sinon库的解决scheme。

一个示例代码来testing:

 /* jshint node:true */ module.exports = Test1; function Test1(options) { options = options || {}; } Test1.prototype.executeSync = function() { console.log("ABC"); console.log("123"); console.log("CBA"); console.log("321"); }; Test1.prototype.executeASync = function(time, callback) { setTimeout(function() { console.log("ABC"); console.log("123"); console.log("CBA"); console.log("321"); callback(); }, time); }; 

摩卡testing:

 /* jshint node:true */ /* global describe:true, it:true, beforeEach:true, afterEach:true, expect:true */ var assert = require('chai').assert; var expect = require('chai').expect; var sinon = require("sinon"); var Test1 = require("../test"); var test1 = null; describe("test1", function() { beforeEach(function() { sinon.stub(console, "log").returns(void 0); sinon.stub(console, "error").returns(void 0); test1 = new Test1(); }); afterEach(function() { console.log.restore(); console.error.restore(); }); describe("executeSync", function() { it("should output correctly", function() { test1.executeSync(); assert.isTrue(console.log.called, "log should have been called."); assert.equal(console.log.callCount, 4); assert.isFalse(console.log.calledOnce); expect(console.log.getCall(0).args[0]).to.equal("ABC"); expect(console.log.getCall(1).args[0]).to.equal("123"); expect(console.log.args[2][0]).to.equal("CBA"); expect(console.log.args[3][0]).to.equal("321"); }); }); describe("executeASync", function() { it("should output correctly", function(done) { test1.executeASync(100, function() { assert.isTrue(console.log.called, "log should have been called."); assert.equal(console.log.callCount, 4); assert.isFalse(console.log.calledOnce); expect(console.log.getCall(0).args[0]).to.equal("ABC"); expect(console.log.getCall(1).args[0]).to.equal("123"); expect(console.log.args[2][0]).to.equal("CBA"); expect(console.log.args[3][0]).to.equal("321"); done(); }); }); }); }); 

我提供上述,因为它演示了使用asynchronous调用,它处理控制台和错误输出,检查的方法是更多的使用。

我应该注意到,我提供了两种获取传递给控制台console.log.getCall(0).args[0]console.log.args[0][0] 。 第一个参数是写入控制台的行。 随意使用你认为合适的东西。

另外两个帮助我的是test-console和intercept-stdout我没有使用拦截标准输出(stdout),但是你可以通过testing控制台来实现。

 var myAsync = require('my-async'); var stdout = require('test-console').stdout; describe('myAsync', function() { it('outputs something', function(done) { var inspect = stdout.inspect(); myAsync().then(function() { inspect.restore(); assert.ok(inspect.output.length > 0); done(); }); }); }); 

注意:您必须使用Mocha的asynchronousAPI。 没有调用done()会吞下mocha的testing消息。