Mokei 0.0.3

Mokei 0.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Tatsuya Tanaka, Tatsuya Tanaka.



Mokei 0.0.3

Mokei

An easy-to-use library to stub HTTP requests.

Specifically, a response of URLSession can be replaced with JSON strings.

How to use

XCTest

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

Quick & Alamofire

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

Installation

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

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