【问题标题】:Why won't these chai tests fail?为什么这些柴测试不会失败?
【发布时间】:2017-09-17 16:01:33
【问题描述】:

我们使用 spectron 和 WebdriverIO 对电子应用程序进行了一些简单的“这是否真的有效”的 chai 测试。我们开始的示例代码来自

https://github.com/jwood803/ElectronSpectronDemo 正如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();
});

现在进入 electron、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!

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

【问题讨论】:

    标签: node.js electron bdd chai spectron


    【解决方案1】:

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

    你会看到你需要从每个测试中返回承诺。这是异步 chai/mocha 测试的典型情况:

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

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

    【讨论】:

    • 我不认为 webdriverio 有错。当我忘记返回承诺时,这看起来就像我在异步 mocha 测试中观察到的行为。
    • @Rhayene - 正确,我试图通过编辑来澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多