为什么这些柴测试不会失败?

Why won't these chai tests fail?

我们使用 spectron 和 WebdriverIO 对电子应用程序进行了一些简单的 "is this really working" chai 测试。我们开始的示例代码来自

https://github.com/jwood803/ElectronSpectronDemo as reported in https://github.com/jwood803/ElectronSpectronDemo/issues/2,chai-as-promised 测试没有捕捉到不匹配,所以我想我会添加一些额外的测试来找出为什么 Chai 没有失败电子应用程序有文本的测试'与预期的单元测试文本匹配。

让我们从非常简单的事情开始,其余代码在https://github.com/drjasonharrison/ElectronSpectronDemo

describe('Test Example', function () {
    beforeEach(function (done) {
        app.start().then(function() { done(); } );
    });

    afterEach(function (done) {
        app.stop().then(function() { done(); });
    });

    it('yes == no should fail', function () {
        chai.expect("yes").to.equal("no");
    });

    it('yes == yes should succeed', function () {
        chai.expect("yes").to.equal("yes");
    });

第一次单元测试失败,第二次成功。

当我们将断言放入函数时,它仍然检测到失败:

it('should fail, but succeeds!?', function () {
    function fn() {
        var yes = 'yes';
        yes.should.equal('no');
    };
    fn();
});

所以现在进入电子、webdriverio 和 spectron 的世界,应用程序标题应该是 "Hello World!",所以这应该会失败,但它通过了:

it('tests the page title', function () {
    page.getApplicationTitle().should.eventually.equal("NO WAY");
});

嗯,让我们来做一个更熟悉的测试:

it('should fail, waitUntilWindowLoaded, yes != no', function () {
    app.client.waitUntilWindowLoaded().getTitle().then(
        function (txt) {
            console.log('txt = ' + txt);
            var yes = 'yes';
            yes.should.equal('no');
        }
    );
});

输出:

    ✓ should fail, waitUntilWindowLoaded, yes != no
txt = Hello World!

成功了吗?什么?为什么?怎么样?

找到了!如果你看 https://github.com/webdriverio/webdriverio/blob/master/examples/standalone/webdriverio.with.mocha.and.chai.js

您会发现您需要 return 每个测试的承诺。这是典型的异步 chai/mocha 测试:

it('tests the page title', function () {
    return page.getApplicationTitle().should.eventually.equal("NO WAY");
});

如果你这样做,那么 chai 测试实际上是正确评估的。