Session 417
๐ Testing Tips & Tricks - WWDC 2018 - Videos - Apple Developer
Notification ์ 1๋ ๋ค๋ก ์ํตํ๋ ๋ฉ์ปค๋์ฆ์ด๋ค. ๊ทธ๋์ ์๋ํ์ง ์์ ์ฌ์ด๋ ์ดํฉํธ๋ฅผ ํผํ๊ธฐ ์ํด ๋ถ๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ํ ์คํธ ํ ํ์๊ฐ ์๋ค.
class PointsOfInterestTableViewController {
var observer: AnyObject?
init() {
let name = CurrentLocationProvider.authChangedNotification
observer = NotificationCenter.default.addObserver(forName: name, object: nil,
self?.handleAuthChanged()
}
}
var didHandleNotification = false
func handleAuthChanged() {
didHandleNotification = true
}
}
default NotificatonCenter๋ฅผ ํตํด observer๋ฅผ ๋ฑ๋ก ํด์ฃผ๊ณ ์๊ณ ์ค์ ๋ก notification์ ๋ฐ์๋์ง flag๊ฐ์ ํตํด ํ์ธ ํด์ฃผ๊ณ ์๋ค.
class PointsOfInterestTableViewControllerTests: XCTestCase {
func testNotification() {
let observer = PointsOfInterestTableViewController()
XCTAssertFalse(observer.didHandleNotification)
let name = CurrentLocationProvider.authChangedNotification
NotificationCenter.default.post(name: name, object: nil)
XCTAssertTrue(observer.didHandleNotification)
}
}
ViewController๊ฐ ์ฌ์ฉํ๋ NotificationCenter
์ post ํด์ฃผ๊ณ ์๋ค.
UIApplication
๊ณผ appDidFinishLaunching
๊ฐ์ notification์ ์ฌ๋ฌ ๋ ์ด์ด์์ observe ํ๊ณ ์๋ค.
๊ทธ๋์ ์ฌ์ด๋์ดํฉํธ๊ฐ ์๊ธฐ๊ฑฐ๋, ํ
์คํธ๊ฐ ๋๋ ค ์ง ์๊ฐ ์๋ค.
โ ํ ์คํธ๋ฅผ ๋ถ๋ฆฌํด ์ฃผ์ด์ผ ํ๋ค.
NotificationCenter ์ ์ฌ๋ฌ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ง ์ ์๋ค. ํด๋์ค ํ๋กํผํฐ์ธ default ์ธ์คํด์ค๋ฅผ ๊ฐ๊ณ ์์ง๋ง, ํ์์์ ์ถ๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑ ํ ์ ์๋ค. ์ด๊ฒ์ด ํ ์คํธ๋ฅผ ๋ถ๋ฆฌ ์ํค๋ ๊ฒ์ ํต์ฌ์ด๋ค.
.default
์ธ์คํด์ค ๋์ ์ฌ์ฉํNotificationCeneter
๋ฅผ ์์ฑ -> Dependency Injection
class PointsOfInterestTableViewController {
let notificationCenter: NotificationCenter
var observer: AnyObject?
init(notificationCenter: NotificationCenter = .default) {
self.notificationCenter = notificationCenter
let name = CurrentLocationProvider.authChangedNotification
observer = notificationCenter.addObserver(forName: name, object: nil,
self?.handleAuthChanged()
}
}
var didHandleNotification = false
func handleAuthChanged() {
didHandleNotification = true
}
}
init ์ unit tests์์ ํ๋ผ๋ฏธํฐ๋ก NotificatioCenter
์ ๋ฐ์ ์ ์๊ฒ ์์ .
class PointsOfInterestTableViewControllerTests: XCTestCase {
func testNotification() {
let notificationCenter = NotificationCenter()
let observer = PointsOfInterestTableViewController(notificationCenter:
notificationCenter)
XCTAssertFalse(observer.didHandleNotification)
let name = CurrentLocationProvider.authChangedNotification notificationCenter.post(name: name, object: nil)
XCTAssertTrue(observer.didHandleNotification)
}
}
ํ
์คํธ ์ฝ๋๋ NotificationCenter
์ ๋ฐ๋ก ์์ฑํ๋๋ก ์์
๋ค๋ฅธ ํด๋์ค๋ SDK ๋ฑ๊ณผ ์ํธ์์ฉ ํ๋ ํด๋์ค๋ ์ธ๋ถ ํด๋์ค๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์ด๋ ต๊ฑฐ๋ ํน์ ์์ ๋ถ๊ฐ๋ฅ ํ๊ธฐ ๋๋ฌธ์, ํ ์คํธ ์์ฑํ๊ธฐ๊ฐ ์ด๋ ต๋ค. ํนํ ์ง์ ์์ฑ ํ ์ ์๊ฒ ๋์์ธ ๋ API, Delegate ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ API ์์ ์ด๋ ค์. ์ธ๋ถ ํด๋์ค์์ ์ํธ์์ฉ์ mockingํ๊ณ ํ๋กํ ์ฝ์ ์ฌ์ฉํด ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ์ ์๋ค. ํ ์คํธ๋ฅผ ๋ ์์กด์ ์ผ๋ก ๋ง๋ค๋ฉด์.
func application(_ application: UIApplication, didFinishLaunchingWithOptions opts: ...) -> Bool {
let isUnitTesting = ProcessInfo.processInfo.environment["IS_UNIT_TESTING"] == "1"
if isUnitTesting == false {
// Do UI-related setup, which can be skipped when testing
}
return true
}