Jasmine:如何在事件触发后运行一系列测试

时间:2015-08-15 10:03:54

标签: node.js jasmine eventemitter jasmine-node

我正在尝试在我的npm模块上运行测试。如果我注释掉2个it块中的任何一个,则下面的代码将起作用,但如果我将它们都保留在下面,则会超时。如何在运行我的测试之前等待“准备好”(我想添加更多,但他们也需要等待“准备好”)?

describe("GA analytics", function() {

    var report = new Report(private_key, SERVICE_EMAIL, 1);

    it("should connect and emit <ready>", function(done) {
        report.on('ready', function() {
            console.log("test.js: Token: ", report.token);
            expect(report.token).toBeDefined();
            done();
        });
    });

    it("should get data correctly", function(done) {
        report.on('ready', function() {
            report.get(query, function(err, data) {
                if (err) throw err
                expect(data.rows).toEqual([ [ '5140' ] ]);
                done();
            });
        });
    });
});

1 个答案:

答案 0 :(得分:0)

我想这种情况正在发生,因为每个测试文件只创建一个Report的新实例,因此ready事件只触发一次而且只触发第一个it块测试将捕获它并处理。剩余的it块不再接收ready个事件,因此他们默默等待,直到Jasmine超时。解决方案是在每个Report块之前创建一个新的it实例,这可以在Jasmine beforeEach的帮助下轻松完成:

describe("GA analytics", function() {

    var report;

    beforeEach(function () {
        report = new Report();
    });

    // ....
});

See the working example here on Plunker(打开&#34; script.js&#34;文件)