Latest 0.0.2
Homepage https://github.com/tattn/Replacer
License MIT Copyright (c) 2017 Tatsuya Tanaka
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Platforms ios 8.0, osx 10.10, tvos 9.0
Dependencies Replacer
Frameworks XCTest, Replacer
Authors

Replacer

Build Status
Carthage compatible
Version
Platform
License
Swift Version

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

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

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

Method Swizzling

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

Installation

Carthage

github "tattn/Replacer"

CocoaPods

pod 'Replacer'

Documentation

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

Replacer is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "TestReplacer",
    "version": "0.0.2",
    "summary": "An easy-to-use library to stub HTTP requests using URLSession and to swizzle methods",
    "homepage": "https://github.com/tattn/Replacer",
    "social_media_url": "https://github.com/tattn/Replacer",
    "authors": {
        "Tatsuya Tanaka (tattn)": "[email protected]"
    },
    "source": {
        "git": "https://github.com/tattn/Replacer.git",
        "tag": "0.0.2"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "frameworks": [
        "XCTest",
        "Replacer"
    ],
    "dependencies": {
        "Replacer": [
            "= 0.0.2"
        ]
    },
    "source_files": "Sources/TestReplacer/*.swift",
    "license": {
        "type": "MIT",
        "text": "                    Copyright (c) 2017 Tatsuya Tanakan                        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:n                          The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.n                              THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.n"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This