1

你能帮我解决摩卡测试中的同步问题吗?

我在 mocha 中进行了这个测试,我使用wd-sync类似于包。

wd-sync 是为了避免 Promises 和回调地狱,它是一个很棒的包,但我发现它有一些问题。它不等待相似,所以这部分是在测试用例之外执行的。我尝试向 Promise 添加类似但没有成功,所以现在我没有想法了。

这是我来自 mocha 的日志- 如您所见,console.log(inside)在测试用例之外执行PROMISE_002,测试通过,但它应该失败。After all hook失败,因为expected在这一步执行。

  Screenshot functionality
1
2
3
4
    ✓ PROMISE_002 (3714ms)
inside
inside2
    1) "after all" hook


  1 passing (16s)
  1 failing

  1) Screenshot functionality "after all" hook:
     Uncaught AssertionError: expected '0.01' to equal 5

我的测试:

it.only('PROMISE_002', wrap(function(){
    const date = new Date();
    const path = `${date.getFullYear()}${date.getMonth()}${date.getDate()}`;
    const dir = `archiveServer/${path}`;

    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir);
    }

    console.log(1);
    driver.saveScreenshot(`archiveServer/baseline/PROMISE_002.png`);
    console.log(2);
    driver.saveScreenshot(`${dir}/PROMISE_002.png`);
    console.log(3);

    resemble(`${dir}/PROMISE_002.png`)
        .compareTo('archiveServer/baseline/PROMISE_002.png')
        .onComplete(function (data) {
            //if (data.misMatchPercentage > 0.5) {
            console.log('inside');
            data.getDiffImage()
                .pack()
                .pipe(fs.createWriteStream(`${dir}/PROMISE_002-diff.png`));
            console.log('inside2');
            expect(data.misMatchPercentage).to.equal(5);
        });

    console.log('4');
}));
4

0 回答 0