PracticeEveryday

Should 본문

Test

Should

kimddakki 2022. 6. 20. 13:40
should.js

 - should.js는 node.js에서 사용할 수 있는 표현적이고 가독성 있으며 테스트 프레임워크에 의존적이지 않은

   단언문 ( Assertion) 라이브러리입니다.

 - 객체의 행동을 enumerable( 열거할 수 있는 ) 않은 단일 getter를 통해 Object를 prototype 확장해 제공하고 있습니다.

 

 - 사실 should.js는 node.js에서 제공하는 Assert 모듈을 확장했기 때문에 assert 모듈과 동일합니다.

 - 즉 should.equal ( str, 'foo' )는 assert.equal ( str, 'foo' ) 와 동일하게 동작하고 should.AssertionError는

   assert.AssertionError와 같습니다. 그래서 node.js의 assert 모듈을 지원하는 어떤 테스트 프레임워크에서도 should.js를 

   사용할 수 있습니다.

 

1. should.js 소스를 사용해야 하므로 설치를 진행합니다.

npm install should
yarn add should

이후의 예제는 소스에서 var should = require('should');로 불러왔다는 가정하에 설명합니다.

 

should.js의 추가 메서드

 - 앞서 이야기했듯 require('should')라 리턴하는 객체는 require('assert')가 리턴한 객체를 확장한 것과 같고

   assertion을 더 표현적이고 가독성 좋게 하는 추가적인 메서드를 제공하고 있습니다.

 

● exist ( 정적 )

should.strictEqual(foo, bar)
foo.should.equal(bar)

 - 위 두 코드는 동일합니다.

 - should.strictEqual(foo, bar ) 는 assert.strictEqual( foo, bar )과 동일합니다.

   하지만 should는 Object prototype을 확장했기 떄문에 2번 라인과 같이 사용 가능 합니다.

   => 더 가독성 있게 만들어 줍니다.

 - 하지만 foo가 존재하지 않는 경우에는 foo is null or undefined라는 오류가 발생할 것입니다.

 - 객체나 프로퍼티의 존재를 알 수 없는 경우에는 이와같이 사용 할 수가 없습니다.

  => 객체나 프로퍼티의 존재 여부는 다음과 같이 exist 메서드로 확인할 수 있습니다.

 

should.exist({})
should.exist([])
should.exist('')
should.exist(0)
should.exist(null)      // will throw
should.exist(undefined) // will throw

 - 다음과 같이 not을 이용해 부정문으로 사용할 수도 있습니다.

should.not.exist(undefined)
should.not.exist(null)
should.not.exist('')    // will throw
should.not.exist({})    // will throw

● ok

 - assertion의 진실성을 ok로 검사합니다.

 - 이는 소스 내부에서 그냥 전달한 객체 obj로만 검사합니다.

 - obj == true가 아니라 그냥 obj로 검사한다는 의미입니다. 

true.should.be.ok
'yay'.should.be.ok
(1).should.be.ok

false.should.not.be.ok
''.should.not.be.ok
(0).should.not.be.ok

 

  true / false

 - true와 false는 다음과 같이 사용합니다

 - 이는 Assert === true 나 Assert === false로 검사합니다.

true.should.be.true
'1'.should.not.be.true

false.should.be.false
(0).should.not.be.false

 

● arguments

 - arguments는 Arguments의 인스턴스인지를 검사합니다.

var args = (function(){ return arguments; })(1,2,3);
args.should.be.arguments;
[].should.not.be.arguments;

 

  empty

 - empty는 객체의 length가 0인지 검사합니다.

[].should.be.empty
''.should.be.empty
({ length: 0 }).should.be.empty

 

 equal

 - strict하게 동등성을 비교합니다. 비교는 ===으로 검사합니다.

  it("equal", () => {
    const value = undefined;
    should.strictEqual(undefined, value);
    should.strictEqual(false, value);
    (4).should.equal(4);
    "test".should.equal("test")[(1, 2, 3)].should.not.equal([1, 2, 3]);
  });

 

whitin

 - 숫자의 밤위를 포함하는지 검사합니다.

 

  it("within", () => {
    const user = {
      age: 30,
    };
    user.age.should.be.within(5, 50);
  });

 

a

 - 객체의 typeof를 검사합니다.

user.should.be.a('object')
'test'.should.be.a('string')

 

instanceof

 - instanceof를 검사합니다.

user.should.be.an.instanceof(User)
[].should.be.an.instanceof(Array)

 

above

 - 주어진 값이 지정한 값보다 큰 지를 검사합니다.

user.age.should.be.above(5)
user.age.should.not.be.above(100)

 

below

 - 주어진 값이 지정한 숫자 값보다 작은 지를 검사합니다.

 - regexp.exec로 검사합니다.

user.age.should.be.below(100)
user.age.should.not.be.below(5)

 

match

 - 정규표현식의 match 여부를 검사합니다.

username.should.match(/^\w+$/)

 

length

 - length 프로퍼티가 존재하는지 검사하고 주어진 숫자값인지 검사합니다.

 - lengthOf는 length의 별칭입니다.

  it("length", () => {
    const arr = [1, 2, 3];
    arr.should.be.length(3);
  });

 

property

 - property에 첫 파라미터로 전달한 이름의 프로퍼티가 존재하는지 검사하고 두 번째 파라미터를 전달하면

   해당 프로퍼티가 두 번째 파라미터 값인지 검사합니다.

  it("property", () => {
    const user = {
      name: "kim",
      age: 30,
    };

    user.should.have.property("name");
    user.should.have.property("name", "kim");
  });

 

 

should.js : node.js에서 사용할 수 있는 BDD 스타일의 Assertion 모듈 :: Outsider's Dev Story

몇일 전에 mocha 테스트 프레임워크를 소개한 관계로 mocha에서 사용할 수 있는 assetion 모듈인 should.js를 정리해 봅니다. mocha 글에서도 썼듯이 mocha는 꼭 should.js를 써야하는 것은 아니지만 사용법도

blog.outsider.ne.kr

 

'Test' 카테고리의 다른 글

TypeScript + Mocha  (0) 2022.08.20
Supertest  (0) 2022.06.12
Supertest  (0) 2022.06.11
Jest  (0) 2022.06.10
JEST  (0) 2022.06.10
Comments