TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✓ |
Maintained by Tatsuya Tanaka, Tatsuya Tanaka.
An easy-to-use library to stub HTTP requests.
Specifically, a response of URLSession can be replaced with JSON strings.
import XCTest
class MokeiTests: XCTestCase {
func testJSONFile() {
// register a stub
self.stub.url("echo.jsontest.com").json(["test": "data"])
// load sample.json & register a stub.
self.stub.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)
}
}
import Quick
import Nimble
import Alamofire
class MokeiSpecs: QuickSpec {
override func spec() {
describe("Quick compatibility test") {
context("using JSON file") {
beforeEach() {
// wait for 1s
self.stub.url("echo.jsontest.com/[a-z]+/.*").httpMethod(.post).json(["test": "data"]).delay(1)
}
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"))
}
}
}
}
}
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
Mokei is released under the MIT license. See LICENSE for details.