Replacer 0.1.0

Replacer 0.1.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2019 年 9 月
SPM支持 Swift Package Manager

Tatsuya TanakaTatsuya Tanaka 维护。



Replacer 0.1.0

Replacer

Build Status Carthage compatible Version Platform License Swift Version

Replacer 是一个用于模拟 HTTP 请求和替换方法的易于使用的库。

具体来说,可以通过它替换 URLSession 的响应,比如任何 JSON,Data 等等。
它使用 方法链式调用 来设置模拟。

如何使用

模拟 HTTP 请求

XCTest

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)
    }
}

快速使用 & Alamofire

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")
    }
}

安装

Carthage

github "tattn/Replacer"

CocoaPods

pod 'Replacer'

文档

贡献

  1. 将它分支
  2. 创建您的功能分支: git checkout -b my-new-feature
  3. 提交您的更改: git commit -am 'Add some feature'
  4. 推送到分支: git push origin my-new-feature
  5. 提交一个拉取请求 :D

许可协议

Replacer遵循MIT许可证发布。有关详细信息,请参阅LICENSE文件。