茉莉花测试无法兑现承诺

时间:2018-09-24 01:35:25

标签: javascript jasmine

我正在编写一个茉莉花测试,该测试在按下按钮后会打开一个对话框? 在代码库中,press函数调用openNewDialog函数。在此函数内部,有一个对辅助函数的调用,该函数进行一个后端调用以检索某些数据。模拟该函数以返回数组并避免真正的后端调用。 解决onPromisHelper.getSomeInfo().之后,它将调用init函数来初始化对话框,然后打开对话框。 我遇到的问题是,在茉莉花测试中打开对话框的检查发生在onPromisHelper.getSomeInfo().的结果返回之前。我不确定如何处理这种情况。知道如何让茉莉花测试等到onPromisHelper.getSomeInfo()解决吗?

var createDialogButton = // some code to retrieve the button
    createDialogButton.firePress(); //fire press eventuall calls openNewDialog. 

> //openNewDialog calls to a helper function that returns a new dialog
>// which being mocked to return a simple dialog  //openNewDialog also
> //makes an API call to get some info which is being spied as following


spyOn(onPromiseHelper, "getSomeInfo").and.returnValue(Promise.resolve([
    {
        displayName: "firstLocation",
        id: "firstLocation"
    },
    {
        displayName: "scondLocation",
        id: "firstLocation"
    }

]));



expect(newDialog.isOpen()).toBeTruthy(); // the problem is this line being executed right after fire press, before all the function inside openNewDialog function gets executed. so this check fails.

openNewDialog的代码

openNewDialog: function() {

     var dialog = // some code creates a dialog

     onPromisHelper.getSomeInfo().then((oSuccessResult) => {
        dialog.init(oSuccessResult);
        dialog.openDialog();
    }).catch((oFail) => {
        console.log(oFail);
    });

    },

0 个答案:

没有答案