All Projects → tattn → Replacer

tattn / Replacer

Licence: mit
An easy-to-use library to stub HTTP requests using URLSession and to swizzle methods

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Replacer

Sbtuitesttunnel
Enable network mocks and more in UI Tests
Stars: ✭ 215 (+616.67%)
Mutual labels:  stub, xctest
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+263.33%)
Mutual labels:  stub, xctest
phake
PHP Mocking Framework
Stars: ✭ 464 (+1446.67%)
Mutual labels:  stub
Dyson
Node server for dynamic, fake JSON.
Stars: ✭ 814 (+2613.33%)
Mutual labels:  stub
Swiftmockgeneratorforxcode
An Xcode extension (plugin) to generate Swift test doubles automatically.
Stars: ✭ 522 (+1640%)
Mutual labels:  stub
Project Stub
deps
Stars: ✭ 300 (+900%)
Mutual labels:  stub
Amber
Reflective PE packer.
Stars: ✭ 594 (+1880%)
Mutual labels:  stub
Perfect-Server-Side-Swift iOS-App
A family tree API server implementation with iOS client. Server has been implemented with Perfect: Server-Side Swift And iOS client is in pure Swift.
Stars: ✭ 15 (-50%)
Mutual labels:  xctest
Clj Fakes
An isolation framework for Clojure/ClojureScript.
Stars: ✭ 26 (-13.33%)
Mutual labels:  stub
Xctesthtmlreport
Xcode-like HTML report for Unit and UI Tests
Stars: ✭ 489 (+1530%)
Mutual labels:  xctest
Pxctest
Execute tests in parallel on multiple iOS Simulators
Stars: ✭ 800 (+2566.67%)
Mutual labels:  xctest
Snap.swift
Snapshot testing in a snap 🎨
Stars: ✭ 420 (+1300%)
Mutual labels:  xctest
Mockingbird
A convenient mocking framework for Swift
Stars: ✭ 302 (+906.67%)
Mutual labels:  xctest
Ts Mockito
Mocking library for TypeScript
Stars: ✭ 608 (+1926.67%)
Mutual labels:  stub
Strictly fake
Stub that automatically verifies that stubbed methods exist and the signatures match the original.
Stars: ✭ 18 (-40%)
Mutual labels:  stub
gidevice
communicate with iOS devices implemented with Golang
Stars: ✭ 156 (+420%)
Mutual labels:  xctest
Fako
Struct Faker for Go
Stars: ✭ 329 (+996.67%)
Mutual labels:  stub
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+16003.33%)
Mutual labels:  stub
Njrat 0.7d Stub Csharp
njRAT C# Stub - Fixed For PowerShell
Stars: ✭ 28 (-6.67%)
Mutual labels:  stub
Xctestextensions
XCTestExtensions is a Swift extension that provides convenient assertions for writing Unit Test.
Stars: ✭ 19 (-36.67%)
Mutual labels:  xctest

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].