Apr 15, 2021, TIL (Today I Learned)

Inwoo Hwang·2021년 8월 26일
0
post-thumbnail

학습내용


궁금증 1: XCTest에서 setUpWithError(), tearDownWithError()는 왜 추가 됐지?

XCode 11.4버전부터 Testing에 새로 추가 된 기능이 있습니다. 기존에는 테스트 시작 전에는 단순히 테스트 시작 준비 그리고 테스트 후 데이터를 cleanUp 해주는데 이제는 에러처리까지 가능해졌더라구요.

이 부분에 대한 이해가 많이 부족한 것 같아서 공식문서를 많이 참고 해 봤습니다.

SetUpWithError()

Before each test begins, XCTest calls setUpWithError(), followed by setUp(). If state preparation might throw errors, override setUpWithError(). XCTest marks the test failed when it catches errors, or skipped when it catches XCTSkip.

tearDownWithError()

Provides an opportunity to perform cleanup and to throw errors after each test method in a test case ends.

읽어보니까 SetUpWithError()부분에서 에러처리 후 테스트시작준비를 할 수 있습니다.

import XCTest
@testable import Expo1900
class Expo1900Tests: XCTestCase {
    private var sut_expo: Expo?
    private var sut_exhibit: [Exhibit]?
  
override func setUpWithError() throws {
  try super.setUpWithError()
  guard let expoDataAsset = NSDataAsset.init(name: "exposition_universelle_1900"),
  let exhibitDataAsset = NSDataAsset.init(name: "items") else { return }
  sut_expo = try JSONDecoder().decode(Expo.self, from: expoDataAsset.data)
  sut_exhibit = try JSONDecoder().decode([Exhibit].self, from: exhibitDataAsset.data)
}

override func tearDownWithError() throws {
  try super.tearDownWithError()
  sut_expo = nil
  sut_exhibit = nil
	}
}

sut_exposut_exhibit을 사용하려면 data로 만들고 객체로 만든 뒤 테스트를 해야 하는데 이 작업을 하려면 JSON파싱이 필요합니다. 그런데 이 작업을 setUpWithError에서 객체화를 진행할 수 있다는 것을 깨달았습니다🤩 setUpWithError가 에러를 던질 수 있기 때문에 이 안에서는 그냥 try만 사용하면되고 따로 do-catchtry문을 사용할 필요가 없어서 획기적이라는 생각이 들었습니다~

궁금증 2: 특정 ViewController에서 회전 설정 조작을 어떻게 하지?

블로그의 힘은 위대합니다 ㅎㅎ

extension UINavigationController {
  override open var shouldAutorotate: Bool {
    get {
      if let visibleVC = visibleViewController {
        return visibleVC.shouldAutorotate
      }
      return super.shouldAutorotate
    }
  }
}

위와 같이 UINavigationController를 확장 해 주면 뷰컨트롤러에서 아래와 같이 연산프로퍼티로 사용할 수 있는게 신기했습니다.

override open var shouldAutorotate: Bool {
            return false
        }

이렇게 설정하니 특정 viewController에서 세로모드로 고정할 수 있게됩니다!!

궁금증 3: 시뮬레이션 녹화 어떻게 하지?

xcrun simctl io booted recordVideo 동영상이름.파일포멧 //Mac의 root 경로에 저장
xcrun simctl io booted recordVideo ~/저장경로/동영상이름.파일포멧

짱입니다!

추가:

Xcode 12.5 업데이트 이후 시뮬레이터에서 녹화 기능이 추가 되어서 더 이상 위 방법으로 사용하지 않아도 될 듯 싶습니다!

[출처]:

XCTest | Apple Developer Documentation

Understanding Setup and Teardown for Test Methods | Apple Developer Documentation

Xcode 11.4 ) setUpWithError / tearDownWithError, XCTSkipIf / XCTSkipUnless (tistory.com)

Disable Rotation of UIViewController Embedded Into UINavigationController - Apps Developer Blog

Xcode 11.4 ) setUpWithError / tearDownWithError, XCTSkipIf / XCTSkipUnless (tistory.com)

profile
james, the enthusiastic developer

0개의 댓글