简体   繁体   English

在Node.js中使用Mocha进行测试

[英]Testing with Mocha in Node.js

I'm getting really inconsistent behavior in my terminal when console.logging inside of a test I wrote using mocha. 在使用mocha编写的测试中进行console.logging记录时,我在终端中的行为确实不一致。 We are running a node.js server and running socket.io. 我们正在运行一个node.js服务器并正在运行socket.io。 Does the console.log not go to the terminal only some of the time for some reason? 出于某种原因,console.log是否仅在某些时间不会进入终端? I'm really confused about this behavior. 我真的很困惑这种行为。

➜  tests git:(master) ✗ mocha test-chat-server.js
hello world


echo
✓ echos message (68ms)

On Connect Things Should Happen
✓ initial connection events
  disconnected
  i'm here


    2 passing (93ms)

➜  tests git:(master) ✗ mocha test-chat-server.js
  hello world


    echo
      ✓ echos message (61ms)

    On Connect Things Should Happen
      ✓ initial connection events


    2 passing (77ms)

The difference between these two times I ran the mocha test are the console.log statements that appears in the first test run (disconnected, i'm here). 我运行mocha测试的这两次之间的区别是第一次测试运行中出现的console.log语句(已断开连接,我在这里)。 They do not appear in the second test that I ran. 它们没有出现在我进行的第二次测试中。

Edit: posting my test code in response to the comment (thank you!) 编辑:发布我的测试代码以回应评论(谢谢!)

var should = require('should');
var socket = require('socket.io-client')('http://localhost:3000');


describe("echo", function () {
    var server,
        options ={
            transports: ['websocket'],
            'force new connection': true
        };

    it("echos message", function (done) {
        var client = socket.connect("http://localhost:3000", options);

        client.once("connect", function () {
            client.once("echo", function (message) {
                message.should.equal("Hello World");

                client.disconnect();
                done();
            });

            client.emit("echo", "Hello World");
        });
        done();
    });
});

describe("On Connect Things Should Happen", function() {

    it('initial connection events', function() {

        should.exist(socket); 
        socket.open();      
        socket.compress(false).emit('an event', { some: 'data' });

        socket.on('error', function() {
            console.log('error'); 
        }); 

        socket.connect(); 
        socket.on('disconnect', function(connection) {
            console.log('disconnected'); 
            console.log("i'm here");
        });



        socket.on('connect', function(connection) {
            console.log('connected'); 
        });
    });
});

You are falling into the classic node async trap. 您正陷入传统的节点异步陷阱。 Your "things should happen" test sometimes returns before the disconnect event happens and sometimes not. 您的“事情应该发生”测试有时会在断开连接事件发生之前返回,有时则不会。

You need to handle the done function the same way you do in the "echoes message" test. 您需要以与在“回显消息”测试中相同的方式来处理完功能。 Punctually, it should like this: 准时,它应该像这样:

socket.on('disconnect', function(connection) { 
console.log('disconnected'); 
console.log("i'm here"); 
done()});

In general, I'm not sure how much that test makes handling all those different callbacks. 总的来说,我不确定该测试可以处理所有这些不同的回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM