Programing

차이의 주장, 기대 그리고해야 할 것의 차이점은 무엇입니까?

lottogame 2020. 6. 16. 21:26
반응형

차이의 주장, 기대 그리고해야 할 것의 차이점은 무엇입니까?


무엇 사이의 차이 assert, expect그리고 should, 언제 무엇을 사용할 수 있나요?

assert.equal(3, '3', '== coerces values to strings');

var foo = 'bar';

expect(foo).to.equal('bar');

foo.should.equal('bar');

차이점이 문서화되어 있습니다.

세 가지 인터페이스는 서로 다른 스타일의 어설 션을 나타냅니다. 궁극적으로 그들은 동일한 작업을 수행합니다. 어떤 사용자는 한 스타일을 다른 스타일보다 선호합니다. 강조 할 가치가있는 몇 가지 기술적 고려 사항도 있습니다.

  1. assert 및 expect 인터페이스는 수정하지 않지만, 수정 Object.prototype해야합니다. 따라서 변경할 수 없거나 변경하고 싶지 않은 환경에서 더 나은 선택입니다 Object.prototype.

  2. assert 및 expect 인터페이스는 거의 모든 곳에서 사용자 정의 메시지를 지원합니다. 예를 들어 :

    assert.isTrue(foo, "foo should be true");
    expect(foo, "foo should be true").to.be.true;
    

    어설 션이 실패하면 "foo should be true"메시지가 실패한 어설 션과 함께 출력됩니다. should 인터페이스로 사용자 정의 메시지를 설정할 기회가 없습니다.

(역사적 메모 : 오랜 시간 동안이 답변에 커스텀 메시지를 받으려면 expect해결 방법을 사용해야 한다고 언급했습니다 . Aurélien Ribon 은 메시지를 expect두 번째 매개 변수로 전달하는 것이 효과가 있다고 알려주었습니다 . 해결 방법 :이 메시지에 대한 지원을 제공하기 시작한 Mocha 버전을 찾을 수 없었거나 처음으로 문서화 된 버전의 문서를 찾을 수 없었습니다.)

그 주 assert.isTrue(foo), expect(foo).to.be.truefoo.should.be.true사용자 정의 메시지를 사용하지 않는 경우 모든 출력은 다음과 foo === 1:

    AssertionError: expected 1 to be true

따라서 예상 및 인터페이스가 읽기 에 더 좋지만 어설 션이 실패 할 때 한 인터페이스가 다른 인터페이스보다 자연스럽게 정보를 제공하는 것과는 다릅니다. 세 가지 인터페이스와 동일하다이 메시지는, 당신에게 말하지 않는 무엇을 정확하게 당신이있어 값에만 것을 테스트했다 1하지만 당신이 원하는 true. 무엇을 테스트하고 있는지 알고 싶으면 메시지를 추가해야합니다.


이 간단한 예제가 차이점을 분명히하기를 바랍니다.

주장

var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');

모든 경우에 assert 스타일을 사용하면 assert 문의 마지막 매개 변수로 선택적 메시지를 포함 할 수 있습니다. 어설 션이 전달되지 않으면 오류 메시지에 포함됩니다.

참고 기대와 구조의 주장을해야 사용 체인 가능 언어,하지만 그들은 주장이 처음 구성되는 방식에 차이가 있습니다. 해야 할 경우, 경고를 극복하기위한 몇 가지주의 사항과 추가 도구도 있습니다.

배고 있다

var expect = require('chai').expect
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);

Expect를 사용하면 실패한 어설 션 앞에 추가 할 임의의 메시지를 포함시킬 수 있습니다.

var answer = 43;

// AssertionError: expected 43 to equal 42.
expect(answer).to.equal(42);

// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);

부울 또는 숫자와 같이 설명이 아닌 주제와 함께 사용할 때 유용합니다.

할까요

should 스타일은 예상 인터페이스와 동일한 체인 가능 어설 션을 허용하지만 체인을 시작하기 위해 should 속성으로 각 객체를 확장합니다. 이 스타일에는 Internet Explorer와 함께 사용할 때 몇 가지 문제가 있으므로 브라우저 호환성에주의하십시오.

var should = require('chai').should() //actually call the function
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
beverages.should.have.property('tea').with.lengthOf(3);

기대와 해야하는 것의 차이점

First of all, notice that the expect require is just a reference to the expect function, whereas with the should require, the function is being executed.

var chai = require('chai')
, expect = chai.expect
, should = chai.should();

The expect interface provides a function as a starting point for chaining your language assertions. It works on node.js and in all browsers.

The should interface extends Object.prototype to provide a single getter as the starting point for your language assertions. It works on node.js and in all modern browsers except Internet Explorer.

참고URL : https://stackoverflow.com/questions/21396524/what-is-the-difference-between-assert-expect-and-should-in-chai

반응형