qunit中expect()的jasmine 2.0版本

jasmine 2.0 version of expect() in qunit

本文关键字:jasmine 0版本 expect qunit      更新时间:2023-09-26

在QUnit中,您可以指定测试中预期运行的断言数量
即。expect(2)

在Jasmine 2.0中有办法做到这一点吗?尽管他们的文档很清楚,但我似乎在任何地方都找不到详尽的API列表。

我想运行多个异步测试,而不必嵌套它们。例如,第三次测试应该可靠地是最后一次。但是,如果前两个完全失败,则指示灯仍然是绿色的,因为调用了done()

it("Should run callbacks with correct data", function(done){
    //expect(3); //QUnit syntax
    test.fastResponse(function(data){
        expect(data).toEqual({a:1, b:2});
    });
    test.fastResponse(function(data){
        expect(data).toEqual({a:1, b:2});
    });
    test.slowResponse(function(data){
        expect(data).toEqual({a:1, b:2});
        //This should fail if the other two tests didn't run
        done();
    });
});

我建议添加一个callCount变量,并在每次调用回调时将其递增1。然后您可以在呼叫done()之前使用expect(callCount).toBe(x)

另一方面,你也可以使用间谍来实现这一点:

it("Should run callbacks with correct data", function(done){
    var callback = jasmine.createSpy("callback");
    var actuallyDone = function() {
        expect(callback.calls.count()).toBe(2);
        expect(callback.calls.all()[0].args[0]).toEqual({a:1, b:2});
        expect(callback.calls.all()[1].args[0]).toEqual({a:1, b:2});
        done();
    };
    test.fastResponse(callback);
    test.fastResponse(callback);
    test.slowResponse(function(data){
        expect(data).toEqual({a:1, b:2});
        actuallyDone();
    });
});