Mokei 0.0.3

Mokei 0.0.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

Tatsuya TanakaTatsuya Tanaka 维护。



Mokei 0.0.3

Mokei

一个易于使用的库,用于模拟 HTTP 请求。

具体来说,可以替换 URLSession 的响应为 JSON 字符串。

如何使用

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

安装

文档

贡献

  1. fork 它!
  2. 创建您的功能分支: git checkout -b my-new-feature
  3. 提交您的更改: git commit -am 'Add some feature'
  4. 推送到分支: git push origin my-new-feature
  5. 提交 pull request :D

许可证

Mokei 根据 MIT 许可证发布。有关详细信息,请参阅 LICENSE。