2018/12/10 iOS Test Night #9 2 #ios_test_night
(@ktanaka117)
• / @ktanaka117
•
• :
• R&D Swift TDD
• PEAKS iOS
• …
•
• :
Json 🤔
let hogeURL = Bundle(for: type(of: self))
.url(forResource: "Hoge", withExtension: "json")!
let data = try! Data(contentsOf: hogeURL)
let decoder = JSONDecoder()
let hoge = try! decoder.decode(Hoge.self, from: data)
hoge Stub hoge 

Json 😄


hoge
let hoge: Hoge = TestDataHelper
.testData(forResource: "Hoge", ofType: "json")
Json -
class TestDataHelper {
static func testData<T: Decodable>(forResource resource: String, ofType type: String) -> T {
let url = Bundle(for: TestDataHelper.self).url(forResource: resource, withExtension: type)!
let data = try! Data(contentsOf: url)
let decoder = JSONDecoder()
return try! decoder.decode(T.self, from: data)
}
}
Response HTTP Stub🤔
var responses = [authError, successWithAccessToken, success]

stub(condition: isHost("example.com")) { request in
return responses.removeFirst()
}
// ......
// ...
OHHTTPStubs.removeAllStubs()
responses test function …
Response HTTP Stub😄
HTTPStub.activate(
condition: isHost("example.com"),
responses: [authError, successWithAccessToken, success])
// ......
// ...
HTTPStub.deactivate()
responses
Response HTTP Stub -
struct HTTPStub {
static func activate(condition: @escaping OHHTTPStubsTestBlock,
responses: [OHHTTPStubsResponse]) {
var responsesArray = responses
stub(condition: condition) { request in
let response = responsesArray.removeFirst()
return response
}
}
static func deactivate() {
OHHTTPStubs.removeAllStubs()
}
}
:
•
•
•
•
12/14 in 

APIClient 

2019/01/15 in pixiv Inc.

iOS

よく使うテストヘルパーの紹介 #ios_test_night

  • 1.
    2018/12/10 iOS TestNight #9 2 #ios_test_night (@ktanaka117)
  • 2.
    • / @ktanaka117 • •: • R&D Swift TDD • PEAKS iOS
  • 3.
  • 4.
    Json 🤔 let hogeURL= Bundle(for: type(of: self)) .url(forResource: "Hoge", withExtension: "json")! let data = try! Data(contentsOf: hogeURL) let decoder = JSONDecoder() let hoge = try! decoder.decode(Hoge.self, from: data) hoge Stub hoge 

  • 5.
    Json 😄 
 hoge let hoge:Hoge = TestDataHelper .testData(forResource: "Hoge", ofType: "json")
  • 6.
    Json - class TestDataHelper{ static func testData<T: Decodable>(forResource resource: String, ofType type: String) -> T { let url = Bundle(for: TestDataHelper.self).url(forResource: resource, withExtension: type)! let data = try! Data(contentsOf: url) let decoder = JSONDecoder() return try! decoder.decode(T.self, from: data) } }
  • 7.
    Response HTTP Stub🤔 varresponses = [authError, successWithAccessToken, success]
 stub(condition: isHost("example.com")) { request in return responses.removeFirst() } // ...... // ... OHHTTPStubs.removeAllStubs() responses test function …
  • 8.
    Response HTTP Stub😄 HTTPStub.activate( condition:isHost("example.com"), responses: [authError, successWithAccessToken, success]) // ...... // ... HTTPStub.deactivate() responses
  • 9.
    Response HTTP Stub- struct HTTPStub { static func activate(condition: @escaping OHHTTPStubsTestBlock, responses: [OHHTTPStubsResponse]) { var responsesArray = responses stub(condition: condition) { request in let response = responsesArray.removeFirst() return response } } static func deactivate() { OHHTTPStubs.removeAllStubs() } }
  • 10.
  • 11.
  • 12.