测试child_process.exec标准输出 [英] Testing child_process.exec stdout

查看:80
本文介绍了测试child_process.exec标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用mocha测试子进程的输出.

I'm trying to test the output of child process with mocha.

我的测试如下:

var should = require("should"),
    exec = require("child_process").exec;

describe('users', function() {
  describe('andrei', function() {
    exec('id andrei', function(error, stdout, stderr) {
      it('should be part of group dev', function() {
        stdout.should.containEql('dev');
      })
    })
  })
})

我遇到的问题是it从未执行.

The problem I'm having is that the it never gets executed.

我可以交换代码,将exec放在it内,然后使用done来确保事情以正确的顺序运行,但这意味着我必须为该命令运行相同的exec.我想针对孩子的性格测试进行的每项测试.

I could swap the code around, to have the exec inside the it and then use done to ensure things run in the right order, but that would mean I'd have to run the same exec for each test I want to make against the child's stdout.

如何针对子进程的标准输出使用多个it语句?

How can I have multiple it statements against the stdout of a child process?

推荐答案

Mocha并非旨在运行您在问题中显示的代码.您可以认为它分两个阶段运行.在第一阶段,它将读取所有测试文件并执行它们.知道套件中进行哪些测试的方法是立即执行对describe的所有回调,并且每次遇到it时,它都会记录为要在之后运行的测试.在第二阶段,它运行测试.因此,对于要使Mocha可见的测试,它必须在第一阶段看到一个it调用.您显示的代码将阻止这种情况的发生.

Mocha is not designed to run the code you show in your question. You could think of it as running in two phases. In the first phase, it reads all of your test files and executes them. The way it knows what tests you have in your suite is by executing all callback to describe immediately, and each time it encounters it it records is as a test to be run later. In the second phase, it runs the tests. So for a test to be visible to Mocha, it has to have seen an it call for it in the first phase. The code you show will prevent this from happening.

如何针对子进程的标准输出使用多个it语句?

How can I have multiple it statements against the stdout of a child process?

听起来您要针对每个it声明一个.我不确定为什么要针对您的具体情况执行此操作.同一测试中的多个断言完全可以.无论如何,如果必须这样做,则可以使用before钩子:

It sounds like you're aiming for one assertion per it. I'm not sure why you want to do this in your specific case. Multiple assertions in the same test is perfectly fine. At any rate, if you must do it then you could use a before hook:

var should = require("should"),
    exec = require("child_process").exec;

describe('users', function() {
    describe('andrei', function() {
        var captured_stdout;

        before(function (done) {
            exec('id andrei', function (error, stdout, stderr) {
                if (error) done(error); // Handle errors.
                captured_stdout = stdout;
                done();
            });
        });

        it('should be part of group dev', function() {
            captured_stdout.should.containEql('dev');
        });
    });
});

传递给before的回调将在父describe中的所有测试之前运行. captured_stdout中捕获了要测试的数据,然后所有测试都可以访问它并对其进行测试.

The callback passed to before will be run before all the tests in the parent describe. The data to be tested is captured in captured_stdout and then the tests can all access it and test against it.

这篇关于测试child_process.exec标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆