9

Selenium-webdriverテストを作成するために、chai-as-promised + mochaを使用しています。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)

文書化された動作によると、期待が失敗した場合、chai-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()
4

1 に答える 1

7

私はあなたが2つの異なる問題を抱えていると思います:

まず、テストの最後に promise を返す必要があります (たとえばQ.all([...]);、最初のテストではreturn Q.all([...]).

2 つ目は、 をchai-as-promised使用しないと機能しないように思われることですmocha-as-promised

mocha-as-promisedテストが失敗する原因となる 2 つのアサーションの例を次に示します。

/* 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)
    ]);
  });
});
于 2014-03-06T23:12:20.410 に答える