javascript - 如何断言 process.stderr 输出?

标签 javascript node.js testing io tdd

我正在构建一个小的命令行工具,但遇到了测试问题。

如何测试当前进程在stderr中写了“omg”?

process.stderr.write("omg")

最佳答案

如果还没有,请安装 Mocha:

npm install -g mocha

shouldpass.js:

process.stderr.write('omg')

shouldfail.js:

process.stdout.write('not omg on stderr')

测试.js:

var exec = require('child_process').exec
  , assert = require('assert')

describe('run tests', function(){
  it('should pass', function(done) {
    exec('node ./shouldpass.js', function(err, stdout, stderr) {
      assert.equal(stderr, 'omg')
      done()    
    })
  })

  it('should fail', function(done) {
    exec('node ./shouldfail.js', function(err, stdout, stderr) {
      assert.equal(stderr, 'omg')
      done()    
    })
  })    
})

运行:

mocha test.js

关于javascript - 如何断言 process.stderr 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404293/

相关文章:

mysql - Node js : Assign mysql result to requests

javascript - azurestorage 和 NodeJS 中的 getaddrinfo 错误

javascript - 在 Immutable.js Map 中查找嵌套对象的最佳方法

javascript - 我的私有(private)消息不适用于 socket.io

javascript - 在没有 Service Worker 的情况下使用 Cache API 缓存网页

javascript - 无法通过子函数调用从数据库检索 Nodejs MySQl 数据

javascript - 如何通过 XPages 中的 CSJS 访问重复控件中的链接?

unit-testing - 自动软件测试

node.js - Windows 上的 TestCafe 失败,并出现 spawn powershell.exe ENOENT 错误

java - Junit 跳过对函数的调用进入另一个问题