使用 Jasmine 在 nodejs 中测试事件

Testing Events in nodejs using Jasmine

我来自嵌入式 C 背景,我对 node.js 或面向对象编程的东西不是很熟悉。
我已经编写了一个示例 node.js 应用程序来在控制台上打印 Hello WorldBye Bye World 并使用事件发射器在两个打印件上发出事件。

我有以下目录结构:

TestScripts/  
├── helloworld.js 
├── index.js 
├── node_modules
│   └── jasmine 
├── package.json 
└── spec
    ├── support
    └── TddHelloWorld_spec.js

helloworld.js

var events = require('events');
var util = require('util');

var SayHello = function() {
    var self = this;

    SayHello.prototype.hello = function(){
        console.log("Hello World");
        if (self.emit) self.emit("SendingHelloToWorld");
    }
    SayHello.prototype.bye = function(){
        console.log("Bye Bye Cruel World!!");
        if (self.emit) self.emit("SendingByeByeToWorld");
    }
}

util.inherits(SayHello, events.EventEmitter);
module.exports = SayHello;

index.js

var HelloWorld = require('./helloworld.js');

var HelloWorldInstance = new HelloWorld();


HelloWorldInstance.on("SendingByeByeToWorld", function(){
    console.log("Got Bye Event");
});

HelloWorldInstance.on("SendingHelloToWorld", function(){
    console.log("Got Hello Event");
});
HelloWorldInstance.hello();
HelloWorldInstance.bye();  

TddHelloWorld_spec.js

describe('Call Hello World', function() {
    var HelloWorld = require('../helloworld.js');    
    var HelloWorldInstance = new HelloWorld();


    beforeEach(function(done) {
        done();
    });
    afterEach(function(done) {
        done();
    });

    it('should initialize WiFi via Connman', function(done) {
        HelloWorldInstance.hello();
        done();
    });

    it('should Call bye bye function', function(done) {
        HelloWorldInstance.bye();
        done();
    });
    it('should listen for Event', function() {
        HelloWorldInstance.on("SendingHelloToWorld", function() {
            console.log("Hit Event");
        });
    });

    it('should listen for Event', function() {
        HelloWorldInstance.on("SendingByeByeToWorld", function() {
            console.log("Hit Bye Bye Event");
        });
    });
});

package.json

{
  "name": "testscripts",
  "version": "1.0.0",
  "description": "Sample Project for Testing",
  "main": "index.js",
  "dependencies": {
    "jasmine": "^3.1.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "jasmine",
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC"
}  

我面临的问题是,当我 运行 我的测试使用 npm test 时,测试用例以相反的顺序执行,而且无法保证用于事件处理的测试用例的执行。
如果我再次执行 npm test 那么可能会执行事件处理的测试用例,但它不会在每个 运行.
上执行 有时会执行事件处理的测试用例,有时不会。

此外,请告诉我我是否以正确的方式使用 jasmine 测试事件?

这就是我获取测试输出的方式:

将您的测试捆绑在一起。首先附加一个事件侦听器,这是测试,然后调用例如.hello() 触发它。

describe('Call Hello World', function() {
    var HelloWorld = require('../helloworld.js');    
    var HelloWorldInstance = new HelloWorld();

    it('should emit SendingHelloToWorld', function(done) {
        HelloWorldInstance.on("SendingHelloToWorld", function() {
            console.log("Hit Event");
            done();
        });
        HelloWorldInstance.hello();
    });
});