programing

모카 유닛 테스트에 대한 사용자 정의 오류 메시지와 함께 차이 기대치를 제공하는 방법은 무엇입니까?

subpage 2023. 6. 8. 19:51
반응형

모카 유닛 테스트에 대한 사용자 정의 오류 메시지와 함께 차이 기대치를 제공하는 방법은 무엇입니까?

차이의 예상을 이용한 모카 테스트가 있습니다.

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

자, 하나는to.be.greaterThan(0)예상이 실패하고 mocha의 오류 출력이 개발 친화적이지 않습니다.

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

그런 다음 실패한 모카 오류가 인쇄됩니다.

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

모든expect메서드에서 선택적 매개 변수를 사용합니다.message:

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

따라서 귀하의 경우 다음과 같습니다.

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

사용하는 경우should사용자의 주장에 대해 조건이 실패할 경우 기록될 테스트 함수에 문자열을 전달할 수 있습니다.예:

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

저는 이것이 기대와 함께 가능할지 확신할 수 없습니다.API는 그것을 언급하지 않는 것 같습니다.

언급URL : https://stackoverflow.com/questions/45678706/how-to-provide-chai-expect-with-custom-error-message-for-mocha-unit-test

반응형