Skip to content

Instantly share code, notes, and snippets.

@enomoto
Last active January 30, 2019 03:47
Show Gist options
  • Select an option

  • Save enomoto/9e16bc34b70b616718bf91992406b9eb to your computer and use it in GitHub Desktop.

Select an option

Save enomoto/9e16bc34b70b616718bf91992406b9eb to your computer and use it in GitHub Desktop.
Xcode 10.1, Swift 4.1, install [Mockingjay](https://github.com/kylef/Mockingjay) with Cocoapods
// XCTestCase+Stub.swift
import Foundation
import XCTest
import Mockingjay
extension XCTestCase {
/// Web API のリクエストをテストプロジェクトの json でスタブする
///
/// - Parameters:
/// - uriStr: URI
/// - status: ステータスコード
/// - with: 拡張子なしのファイル名
func stub(uri uriStr: String, status: Int = 200, with resource: String) {
let testBundle = Bundle(for: type(of: self))
let path = testBundle.url(forResource: resource, withExtension: "json")
let data: NSData? = try? NSData(contentsOf: path!, options: .uncached)
self.stub(uri(uriStr), jsonData(data! as Data, status: status))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment