개발/TestDouble

TestDouble에 대하여

코르피 2023. 9. 9. 09:20
반응형

TestDouble이란?

스턴트 맨으로 불리는 Stunt Double에서 유래됐다.

원활한 테스트를 위해 도와주는 객체를 말한다.

 

테스트 더블은 크게 5가지로 나뉜다.

 

1. Dummy

더보기

objects are passed around but never actually used. Usually they are just used to fill parameter lists.

더미는 전달되기만 하고 실제로 사용되지는 않는다. 매개변수를 채우기만 하는 용도로 사용된다.

 

2. Fake

더보기

objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).

페이크는 '실제로 동작하는 구현'을 가지고 있지만 몇가지 방법을 사용해서 상품에 맞지않게 만든다.

(인메모리 데이터베이스가 좋은 예 입니다.)

 

예를들어 Fake로 메모리에서만 잠시 사용하고 종료하면 사라지는 데이터베이스를 구현해두면

실제 데이터베이스에 영향이 가지 않기 때문에 테스트에 사용하기 좋다.

 

3. Stub

더보기

provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

테스트 중에 호출되면 준비된 응답을 넘겨준다. 보통 테스트를 위해 준비된 응답 외에는 답하지 않는다.

 

Dummy의 상태에서 함수에 대한 리턴을 준비해놓은 상태라고 할 수 있다.

// Dummy
func work() { }

// Stub
func isChecked(_ uuid: UUID) -> Bool {
    return true
}

Dummy는 사용하기 위해서 사용하는 것.

Stub은 준비된 답변이 있는 것.

 

4. Spy

더보기

Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.

Spy는 Stub에 메서드가 호출될 때 어떤 정보를 저장해두는 것이다. 이 중 한가지는 이메일 서비스 클래스에서 메세지가 보내진 수를 기록하는 것이다.

 

스파이처럼 메서드가 사용된 횟수를 기록해두는 것.

 

 

5. Mock

더보기

objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

직역: Mock은 수신할 것으로 예상되는 호출의 사양을 형성한 기대로 미리 짜여져있다.

....대체 무슨말인지 이해가 안되어서 조금 더 찾아봤는데

 

메서드 입력에 따른 예상되는 동작과 호출을 구현해놓은 가상의 개체이다.

 

 

 

 

 

 

참고

https://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs

 

Mocks Aren't Stubs

Explaining the difference between Mock Objects and Stubs (together with other forms of Test Double). Also the difference between classical and mockist styles of unit testing.

martinfowler.com

 

반응형