2017-08-14 16 views
4

は私がのが期待チャイを使用してモカテストを持っている:mocha unit testにカスタムエラーメッセージを表示する方法を教えてください。

it("should parse sails out of cache file", async() => { 
    const sailExtractor = new Extractor(); 
    const result = await sailExtractor.extract("test.xml"); 

    try { 
     expect(result.length).to.be.greaterThan(0); 
     const withMandatoryFlight = result.filter((cruises) => { 
      return cruises.hasMandatoryFlight === true; 
     }); 
     expect(withMandatoryFlight.length).to.be.greaterThan(0); 
     const cruiseOnly = result.filter((cruises) => { 
      return cruises.hasMandatoryFlight === false; 
     }); 

     expect(cruiseOnly.length).to.be.greaterThan(0); 

     return Promise.resolve(); 
    } catch (e) { 
     return Promise.reject(e); 
    } 
} 

1つのto.be.greaterThan(0)期待が失敗した場合さて、モカのエラーが出力されないDEV-優しい:

AssertionError: expected 0 to be above 0 
     at Assertion.assertAbove (node_modules/chai/lib/chai/core/assertions.js:571:12) 
     at Assertion.ctx.(anonymous function) [as greaterThan] (node_modules/chai/lib/chai/utils/addMethod.js:41:25) 
     at _callee2$ (tests/unit/operator/croisiEurope/CroisXmlExtractorTest.js:409:61) 
     at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40) 
     at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22) 
     at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21) 
     at fulfilled (node_modules/tslib/tslib.js:93:62) 
     at <anonymous> 
     at process._tickDomainCallback (internal/process/next_tick.js:228:7) 

私はそれを交換したいと思いますより人間的なもの。チャイにカスタマイズエラーメッセージを使用するよう指示する方法はありますか?

私は、この擬似コードのようにそれを使用することができるようにしたい:

expect(result.length) 
     .to.be.greaterThan(0) 
     .withErrorMessage("It should parse at least one sail out of the flatfile, but result is empty"); 

そして失敗モカエラーは印刷する必要があります:あなたはあなたのアサーションのためshouldを使用する場合は

AssertionError: It should parse at least one sail out of the flatfile, but result is empty 

答えて

3

すべてexpect方法は、オプションのパラメータmessage受け入れ

expect(result.length) 
    .to.be.greaterThan(0, "It should parse at least one sail out of the flatfile, but result is empty"); 
+0

OPがchai' '使用しているのが、あなたの答えはに適用されるように見えますそのライブラリも同様です。 Alternateivelty、あなたは 'expect(result.length、 'This is parse ...')を使うことができます。to.be.greaterThan(0)' – robertklep

+0

はい、そうです。 – alexmac

+1

Sidenote:私はtypescriptを使用しているので、その型指定が['NumberComparer'インターフェース](https://github.com/DefinitelyTyped/)として宣言されているので、自分自身が' greaterThan'メソッドにジャンプして自分自身を見つけました。 DefinitelyTyped/blob/9030f8f40fb13aef2242c2d011d98a7a03044083/types/chai/index.d.ts#L157)、オプションのメッセージパラメータが表示されます。 – k0pernikus

0

をすることができます文字列をテスト関数に渡します。この文字列は、条件が失敗した場合に書き出されます。例:

result.length.should.be.above(0, "It should parse at least one sail out of the flatfile, but result is empty"); 

期待通りに可能かどうかわかりません。 APIには言及していないようです。

expect(1).to.be.above(2, 'nooo why fail??'); 
expect(1, 'nooo why fail??').to.be.above(2); 

だから、あなたの場合にはそれがあるべき:

関連する問題