문제

In QUnit you can specify how many assertions are expected to run within a test
i.e. expect(2)

Is there way to do this in Jasmine 2.0? Though their docs are clear, I can't seem to find an exhaustive API list anywhere.

I would like to run multiple async tests without having to nest them. For example, the third test should reliably be last. But if the first two fail completely, the light is still green because done() is called.

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

});
도움이 되었습니까?

해결책

I would recommend adding a callCount variable, and incrementing it by 1 each time a callback is invoked. Then you can expect(callCount).toBe(x) just before the call to done().

On the other hand, you can use Spies to achieve this as well:

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

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top