我正在尝试在我的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();
});
});
});
});
答案 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;文件)