상세 컨텐츠

본문 제목

5. Test Expectations

TIL/TDD iOS

by box-jeon 2019. 11. 26. 09:25

본문

Using an expectation

expectation과 waiter 그리고 fulfil.

Writing an asynchronous test

테스트를 async로 작성해봤을 뿐, 실제로 async 동작을 테스트한 것은 아닙니다. didSet는 synchronous로 동작.

* completion block 내에선 항상 fulfill을 호출하는 것이 좋다. 테스트의 실패 여부는 wait 이후에 XCTAssert를 이용해 판정. Expectation timeout을 테스트 실패로 판단하게 되면 그만큼 테스트 시간이 늘어나는 것.

Testing for true asynchronicity

AppModel.state가 변경되었을 때, 버튼의 title text가 제대로 변경되었는지 테스트합니다. Helper class인 ButtonObserver를 작성해서, 버튼의 title text를 observing. expectation을 argument로 받아 observe 시 fulfill.

AppModel.stateChangedCallback에 StepCountController.updateUI()를 DispatchQueue.main.async로 감싼 것은 방어코드 차원인 듯.

* Debugger를 이용해 break point를 걸어도 expectation.wait의 타이머는 중단되지 않는다.

Waiting for notifications

Notification을 wait.

Building the alert center

Expectation이 Notification을 observing하다가 내부적으로 fulfill()해주는 인터페이스를 제공.

Waiting for multiple events

Alert을 2개 보내는 테스트를 작성했는데, 실제로는 1개만 보내도 테스트가 성공하는 문제가 있음. (아마도) NotificationCenter를 이용해서 구현했을 것 같은데, 2개의 expectation이 동일한 Notification을 observe하고 있기 때문에 첫 번째 Notification에서 각각 fulfill.

Expectation.expectedFulfillmentCount 프로퍼티를 제공해서, 하나의 Expectation이 기대하고 있는 조건의 발생 횟수를 지정할 수 있음.

Expecting something not to happen

동일한 alert이 중복으로 발생할 경우, Notification이 중복으로 발생하면 안된다는 테스트.

Expectation.isInverted 프로퍼티를 제공. true로 설정할 경우, Expectation이 fulfill되면 테스트 실패, wait timeout의 경우 테스트 성공.

 

Showing the alert to a user

RootViewController.alertContainer.isHidden을 확인. AlertCenter.listenForAlerts는 왜 class function으로 구현되었을까?

Continuous refactoring

sut가 각 테스트 시작 전에 제대로 초기화가 되지 않아 테스트가 실패하는 경우가 있으므로 이를 리팩토링. setUp/tearDown에서 처리.

Getting specific abount notifications

Notification 사용 시, userInfo를 이용해 추가적인 정보를 넘기고 이를 assert에 사용.

Driving alerts from the data model

25%, 50%, 75% 달성 시마다 유저에게 독려 얼럿을 표시하려고 한다.

Testing for multiple expectation

25%, 50%, 75%, complete 상황에 대한 expectation을 정의하고, array로 wait하는 방법을 소개. enforceOrder 파라미터를 통해, 정의된 순서대로 expectation이 fulfill될 때에만 최종적으로 fulfill됨.

Refining Requirements

작성한 테스트는 통과되었지만, 실제로 앱을 사용해보면 step 변경 시마다 DataModel.updateForSteps()가 호출될 것이고, 25%가 넘는 시점부터 Alert이 반복적으로 표시될 것. 이를 구현하기 위해 테스트를 먼저 작성하고, AlertCenter.sentAlerts를 추가 구현.

Using other types fo expectations

KeyValueObservingExpectation이 이미 있음. 앞서서 작성한 ButtonObserver를 치환.

- XCTestExpectation

- XCTKVOExpectation

- XCTNSNotificationExpectation

- XCTDarwinNotificationExpectation

- XCTNSPredicateExpectation

 

 

 

 

관련글 더보기

댓글 영역