Merge pull request #5 from tattn/dependabot/bundler/tzinfo-1.2.10 Bump tzinfo from 1.2.5 to 1.2.10
Merge pull request #5 from tattn/dependabot/bundler/tzinfo-1.2.10
Bump tzinfo from 1.2.5 to 1.2.10
Replacer is an easy-to-use library to stub HTTP requests and to swizzle methods.
Specifically, URLSession’s response can be replaced with any JSON, Data, and so on….It uses method chaining to set stubs up.
import Replacer import TestReplacer class SampleTests: XCTestCase { func testJSONFile() { // register a stub self.urlStub.url("echo.jsontest.com").json(["test": "data"]) // load sample.json & register a stub. self.urlStub.json(filename: "sample") let expectation = self.expectation(description: "") let url = URL(string: "http://echo.jsontest.com/key/value/one/two")! URLSession(configuration: .default).dataTask(with: url) { data, _, _ in let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: String] XCTAssert(json["test"] == "data") expectation.fulfill() }.resume() self.waitForExpectations(timeout: 0.02, handler: nil) } }
class SampleSpecs: QuickSpec { override func spec() { describe("Quick compatibility test") { context("using JSON file") { beforeEach() { // wait for 1.5s self.urlStub.url("echo.jsontest.com/[a-z]+/.*") .httpMethod(.post) .json(["test": "data"]) .delay(1.5) } it("returns mock data") { var json: [String: String]? Alamofire.request("http://echo.jsontest.com/key/value/one/two", method: .post).responseJSON { response in json = response.result.value as? [String: String] } // SessionManager is also OK. // SessionManager.default.request("http://echo.jsontest.com/key/value/one/two").responseJSON { _ in } expect(json?["test"]).toEventually(equal("data")) } } } } }
import UIKit import Replacer @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { Replacer.replaceInstance(#selector(UIViewController.viewWillAppear(_:)), of: UIViewController.self, with: #selector(UIViewController.orig_viewWillAppear(_:)), of: UIViewController.self) return true } } extension UIViewController { func orig_viewWillAppear(_ animated: Bool) { orig_viewWillAppear(animated) print("swizzled") } }
github "tattn/Replacer"
pod 'Replacer'
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
Replacer is released under the MIT license. See LICENSE for details.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
Replacer
Replacer is an easy-to-use library to stub HTTP requests and to swizzle methods.
Specifically, URLSession’s response can be replaced with any JSON, Data, and so on….
It uses method chaining to set stubs up.
How to use
Stub HTTP Request
XCTest
Quick & Alamofire
Method Swizzling
Installation
Carthage
CocoaPods
Documentation
Contributing
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
License
Replacer is released under the MIT license. See LICENSE for details.