EmberJS + sinon을 사용한 좋은 어설션 메시지
6545 단어 embersinonjavascripttesting

설정이 다른 꽤 많은 프로젝트EmberJS를 작업하기 때문에 옵션을 문서화하는 것이 좋을 것이라고 생각했습니다.
초기 설정
sinon
프로젝트에서 EmberJS
의 경우 ember-sinon-qunit 을 설치하고 싶을 것입니다. 이렇게 하면 자동으로 ember-sinon 가져옵니다.어설션
일부
sinon
사용법이 있는 다음 테스트 코드를 가정해 보겠습니다.import sinon from 'sinon';
test('my awesome test', function (assert) {
const stub = sinon.stub().named('Awesome spy').returns('hello');
stub('First call');
stub('Second call');
// Place for future assertions
});
큐유닛
기본적으로
qunit
모든 ember-cli
프로젝트에서 사용할 수 있는 assert
기능을 has quite a few methods 제공합니다. 그런 다음 이를 사용하여 sinon
객체가 경험한 것을 검사할 수 있습니다.assert.ok(spy.calledOnce);
다음 오류가 발생합니다.

그것은 ... 차선책입니다. 이 메시지는 실패한 이유에 대한 컨텍스트를 제공하지 않습니다. 함수가 정확히 한 번 호출될 것으로 예상했습니다.
물론 다음과 같이 할 수 있습니다.
assert.ok(stub.calledOnce, 'Expected to be called only once');
그러면 더 좋은 오류가 발생합니다.

그러나 이것은:
더 잘할 수 있을까요?
불씨
ember-sinon 패키지 덕분에
sinon
를 테스트에 직접 가져올 수 있습니다.sinon.assert.calledOnce(stub);
다음과 같은 약간 더 유익한 오류 메시지를 제공합니다.

이것은 우리에게 더 많은 맥락을 제공하기 때문에 더 좋지만:
test
의 유일한 유형인 경우 Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.
오류가 발생합니다. 더 잘할 수 있을까요?
qunit-sinon-assertions
Addonqunit-sinon-assertions은 이전 방법의 단점을 정확히 해결하기 위해 만들어졌습니다. 따라서 이 코드는 다음과 같습니다.
assert
.spy(stub)
.calledOnce()
.calledWith(['John Doe'])
.returnedWith('world');
다음과 같은 오류가 발생합니다.

결론
이해하기 쉬운 주장 메시지가 중요하다고 생각합니다. 이를 달성하기 위한 다양한 방법이 있습니다. 개인적으로 저는 코드를 적게 작성하고 도구에서 제공하는 규칙에 의존하는 것을 선호합니다.
사진 제공 Miguel Á. Padriñán
Reference
이 문제에 관하여(EmberJS + sinon을 사용한 좋은 어설션 메시지), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/michalbryxi/good-assertion-messages-with-emberjs-sinon-3phc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)