为什么这些柴testing不会失败?

我们有一个简单的“这是真正的工作”使用Spectron和WebdriverIO一个电子应用程序的柴testing。 我们开始的示例代码是从

https://github.com/jwood803/ElectronSpectronDemo报告在https://github.com/jwood803/ElectronSpectronDemo/issues/2中 ,chai-as-promisedtesting不捕获不匹配,所以我想我会添加一些额外的testing,以找出为什么柴是不是失败的testing电子应用程序的文本不符合预期的unit testing文本。

让我们从简单的东西开始,其余的代码在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"); }); 

第一次unit testing失败,第二次成功。

而当我们把这个断言放到一个函数中时,它仍能检测到这个失败:

 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"); }); 

嗯,让我们尝试一个更熟悉的testing:

 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! 

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

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

你会看到你需要从每个testing中返回承诺。 这是asynchronous柴/摩卡testing的典型情况:

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

如果你这样做,那么柴testing实际上是正确的评估。