禁用Jasmine测试的Bootstrap转换

时间:2013-09-23 12:40:23

标签: twitter-bootstrap jasmine

我正在尝试编写一个Jasmine测试来覆盖Twitter Boostrap模式对话框。当调试器行被注释掉时,测试失败。它在调试器暂停处理时传递,然后我继续。我相信Bootstrap模式中的转换导致了这个问题,因为在我预期调用时,模态对话框还没有出现在DOM中。

如何在测试期间禁用转换?

由于

describe("test dialog", function(){
    it("when cancel button is clicked", function() {
        spyOn(MyTestObj, 'myMethod')

        $("#cancelButton").click();

        debugger;

        expect($(".bootbox-body")).toHaveText("Are you sure you want to cancel?")

        $('.modal-footer button[data-bb-handler="Yes"]').click();

        expect(MyTestObj.myMethod).toHaveBeenCalledWith("123")
    })
})

感谢Jarred,您的解决方案非常棒!这是我的工作测试:

    describe("test dialog", function(){
        it("when cancel button is clicked", function() {
            spyOn(MyTestObj, 'myMethod')

            $("#cancelButton").click();

            waitsFor(function() {
                return $(".bootbox-body").is(":visible");
            }, "Element did not show up", 1000);

            runs(function () {
                expect($(".bootbox-body")).toHaveText("Are you sure you want to cancel?")
                $('.modal-footer button[data-bb-handler="Yes"]').click();

                expect(MyTestObj.myMethod).toHaveBeenCalledWith("123")
            })
        })      
    })

1 个答案:

答案 0 :(得分:3)

您可以使用waitsFor茉莉花方法,然后使用runs块(https://github.com/pivotal/jasmine/wiki/Asynchronous-specs#waitsforfunction-optional-message-optional-timeout):

$('.modal-footer button[data-bb-handler="Yes"]').click();

waitsFor(function() {
    return $(".bootbox-body").is(":visible");
}, "Element did not show up", 1000);

runs(function () {
    expect(MyTestObj.myMethod).toHaveBeenCalledWith("123")
})

waitsFor(function() { return $(".bootbox-body").is(":visible"); }, "Element did not show up", 1000); runs(function () { expect(MyTestObj.myMethod).toHaveBeenCalledWith("123") })