【问题标题】:Chai-As-Promised is eating assertion errorsChai-As-Promised 正在吃断言错误
【发布时间】:2014-01-22 18:44:27
【问题描述】:

我正在使用 chai-as-promised + mocha 来编写一些 selenium-webdriver 测试。由于 webdriver 广泛使用 promises,我想如果我在这些类型的测试中使用 chai-as-promised 会更好。

问题是,当测试失败时,错误并没有被 mocha 正确捕获,它只是失败而没有输出任何东西。

示例代码:

it 'tests log', (next) ->
  log = @client.findElement(By.css("..."))
  Q.all([
    expect(log.getText()).to.eventually.equal("My Text")
    expect(log.findElement(By.css(".image")).getAttribute('src'))
      .to.eventually.equal("/test.png")
  ]).should.notify(next)

根据documented behaviourchai-as-promised 应该在期望失败时将错误传递给 mocha。对吧?

作为一种变体,

我也试过这些,但没有用:

#2

# same, no error on failure

it 'tests log', (next) ->
  log = @client.findElement(By.css("..."))
  Q.all([
    expect(log.getText()).to.eventually.equal("My Text")
    expect(log.findElement(By.css(".image")).getAttribute('src'))
      .to.eventually.equal("/test.png")
  ]).should.notify(next)

#3

# same, no error shown on failure

it 'tests log', (next) ->
  log = @client.findElement(By.css("..."))
  expect(log.getText()).to.eventually.equal("My Text")
  .then ->
     expect(log.findElement(By.css(".image")).getAttribute('src'))
       .to.eventually.equal("/test.png").should.notify(next)

#4

## DOES NOT EVEN PASS

it 'tests log', (next) ->            
  log = @client.findElement(By.css("..."))
  Q.all([
    expect(log.getText()).to.eventually.equal("My Text")
    expect(log.findElement(By.css(".image")).getAttribute('src'))
      .to.eventually.equal("/test.png")
  ])
  .then ->
    next()
  .fail (err) ->
    console.log(err)
  .done()

【问题讨论】:

    标签: selenium testing mocha.js chai chai-as-promised


    【解决方案1】:

    我认为你有两个不同的问题:

    首先,您需要在测试结束时返回一个承诺(例如,您的第一个测试中的 Q.all([...]); 应该是 return Q.all([...])

    其次,我发现chai-as-promised 似乎不起作用,除非与mocha-as-promised 一起使用。

    这是一个 mocha-as-promised 的示例,其中包含两个将导致测试失败的断言。

    /* jshint node:true */
    
    /* global describe:false */
    /* global it:false */
    
    'use strict';
    
    var Q = require('q');
    
    require('mocha-as-promised')();
    
    var chai = require('chai');
    var chaiAsPromised = require('chai-as-promised');
    chai.use(chaiAsPromised);
    
    var expect = chai.expect;
    
    describe('the test suite', function() {
      it('uses promises properly', function() {
        var defer = Q.defer();
        setTimeout(function() {
            defer.resolve(2);
          }, 1000);
    
        return Q.all([
          expect(0).equals(0),
    
          // the next line will fail
          expect(1).equals(0),
    
          expect(defer.promise).to.eventually.equal(2),
    
          // the next line will fail
          expect(defer.promise).to.eventually.equal(0)
        ]);
      });
    });
    

    【讨论】:

    • 关于第一点,我使用的是coffee-script,所以return对于任何函数的最后一行都是隐含的。
    • 只想指出,如今 mocha-as-promised 似乎是不必要的。根据github.com/domenic/mocha-as-promisedMocha 1.18.0 内置了promise 支持。 mochajs.org 的文档似乎同意。
    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多