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

Jasmine: how to run series of tests after an event has triggered

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

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();
            });
        });
    });
});

我猜这是因为你只为每个测试文件创建了一次 Report 的新实例,因此 ready 事件只触发一次并且只触发第一个 it 块在测试中将捕获它并进行处理。其余的 it 块将不会再收到任何 ready 事件,因此它们静静地等待直到 Jasmine 超时。解决方案是在每个 it 块之前创建一个新的 Report 实例,这可以在 Jasmine beforeEach:

的帮助下轻松完成
describe("GA analytics", function() {

    var report;

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

    // ....
});

See the working example here on Plunker(打开一个 "script.js" 文件)