0

谁能给我一个关于如何在 QUnit 中测试以下代码的指针?

function ChooseBranch()
{
var BranchPromise = getBranches();
BranchPromise.then(function (BranchData) {
    var BranchOptions = [];
    $.each(BranchData, function (i, d) {
        BranchOptions.push(d.BranchDescription)
    });

    swal({
        title: 'Delivery Branch',
        text: 'Please choose the branch below where the customer would like the order delivered',
        showCancelButton: false,
        input: 'select',
        inputOptions: BranchOptions
    }).then(function (result) {
        DeliveryType = "Other";
        DeliverToBranch = BranchData[result].BranchCode;
        ItemOrder();
    });
});

}

我尝试让 sweetalert2 对话框选择第一个选项,但直到测试失败后它才会出现在 dom 中?我基本上想测试 swal 是否可见。

4

1 回答 1

2

看起来您正在寻找 QUnit 的异步 api:https ://api.qunitjs.com/assert/async

ChooseBranch似乎是异步的。如果您希望能够测试该功能,则需要在该异步调用中提供某种挂钩。在这个例子中相当简单,只需BranchPromiseChooseBranch函数中返回。

完成后,您应该能够编写一个 qunit 测试,例如:

Qunit.test('ChooseBranch', function(assert) {
    var done = assert.async();
    var promise = ChooseBranch();
    promise.then(function() {
        // Test that the sweetalert2 dialog was displayed
        done();
    });
});
于 2017-04-05T13:18:38.243 回答