测试child_process.exec会导致一个错误的测试...为什么?

时间:2014-06-29 19:25:51

标签: node.js mocha child-process

Flakey Test

我正在尝试为黄瓜世界模块编写单元测试。该模块似乎正在运行,它将Express应用程序作为child_process启动,并通过终止子进程来停止应用程序。

问题是这个模块的摩卡测试很复杂,它每隔一段时间都会传递,并且每隔一段时间都会失败。我似乎无法弄明白为什么。

如何为每次都会通过的世界文件编写测试?

黄瓜世界档案

var zombie = require('zombie');
var cp = require('child_process');

var World = function World(callback) {
    this.browser = new zombie();
    callback(this);
};

World.prototype.start = function (callback) {
    console.log("Starting Express App");
    this.app = cp.exec('node ./bin/www');
    console.log("Express App Started");
    if (callback) callback();
};

World.prototype.stop = function (callback) {
    this.app.on('close', function () {
        console.log("Stopped Express App");
        if (callback) callback();
    });
    this.app.kill('SIGHUP');
};

World.prototype.visit = function (url, callback) {
    this.browser.visit(url, callback);
};

module.exports = World;

世界模块的Flakey测试

var assert = require('assert'),
http = require('http'),
World = require('../features/support/world');

describe("Cucumber World", function () {

    var world;

    beforeEach(function(done) {
        world = new World(function (world) {
            world.start(done);
        });
    });

    it('should start the web app', function (done) {
        http.get("http://localhost:3000", function (res) {
            assert.ok(res, "A successful response was made");
            assert.equal(res.statusCode, 200, "200 success status received");
            world.stop(done);
        });
    });

});

0 个答案:

没有答案