tattn / Replacer
Licence: mit
An easy-to-use library to stub HTTP requests using URLSession and to swizzle methods
Stars: ✭ 30
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
Swiftmockgeneratorforxcode
An Xcode extension (plugin) to generate Swift test doubles automatically.
Stars: ✭ 522 (+1640%)
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
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
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
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
Xctestextensions
XCTestExtensions is a Swift extension that provides convenient assertions for writing Unit Test.
Stars: ✭ 19 (-36.67%)
Mutual labels: xctest
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
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
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- 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].