source

Chai: '해야' 구문을 사용하여 정의되지 않았는지 테스트하는 방법

nicesource 2023. 3. 5. 09:54
반응형

Chai: '해야' 구문을 사용하여 정의되지 않았는지 테스트하는 방법

이 튜토리얼을 기반으로 chai를 사용한 angularjs 앱 테스트에서 "should" 스타일을 사용하여 정의되지 않은 값에 대한 테스트를 추가하고 싶습니다.실패:

it ('cannot play outside the board', function() {
  scope.play(10).should.be.undefined;
});

"TypeError: Unread property 'should' of undefined"라는 오류가 표시되지만 테스트는 "expect" 스타일로 통과합니다.

it ('cannot play outside the board', function() {
  chai.expect(scope.play(10)).to.be.undefined;
});

'해야 한다'로 작동하려면 어떻게 해야 하나요?

이것은 should 구문의 단점 중 하나입니다.should 속성을 모든 개체에 추가하는 방식으로 작동하지만 반환 값 또는 변수 값이 정의되지 않은 경우 속성을 유지할 개체가 없습니다.

문서에서는 다음과 같은 몇 가지 회피책을 제시합니다.

var should = require('chai').should();
db.get(1234, function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  doc.should.be.an('object');
});
should.equal(testedValue, undefined);

차이 문서에 언급된 바와 같이

(typeof scope.play(10)).should.equal('undefined');

정의되지 않은 테스트

var should = require('should');
...
should(scope.play(10)).be.undefined;

특수한 테스트

var should = require('should');
...
should(scope.play(10)).be.null;

거짓(즉, 조건에서 거짓으로 처리됨)을 검사

var should = require('should');
...
should(scope.play(10)).not.be.ok;

나는 정의되지 않은 시험에 대해 설명문을 쓰느라 애를 먹었다.다음 항목은 작동하지 않습니다.

target.should.be.undefined();

저는 다음과 같은 해결책을 찾았습니다.

(target === undefined).should.be.true()

그것을 타이프 체크로도 쓸 수 있다면

(typeof target).should.be.equal('undefined');

위의 방법이 옳은지 모르겠지만, 효과가 있습니다.

기투브에 있는 유령의 포스트에 따르면

이것을 시험해 보세요.

it ('cannot play outside the board', function() {
   expect(scope.play(10)).to.be.undefined; // undefined
   expect(scope.play(10)).to.not.be.undefined; // or not
});

의 조합을 잊지 마십시오.have그리고.not키워드:

const chai = require('chai');
chai.should();
// ...
userData.should.not.have.property('passwordHash');

설명서에 따르면 @david-norman의 답변은 정확합니다.설정에 몇 가지 문제가 있어서 대신 다음을 선택했습니다.

(type of scope.play(10)).should.be.http://should.be;

기능 결과를 다음과 같이 정리할 수 있습니다.should()및 테스트에 "유형"이 있는지 확인합니다.

it ('cannot play outside the board', function() {
  should(scope.play(10)).be.type('undefined');
});

언급URL : https://stackoverflow.com/questions/19209128/chai-how-to-test-for-undefined-with-should-syntax

반응형