MockFloret

public class MockFloret : InterceptingFloret

The MockFloret helps you to easily mock your network requests for tests or to reproduce a bug.

Recording Requests

let mockFloret = MockFloret(mode: .record)

If you create your MockFloret in recording mode it will record all requests and their responses (depending on the Cauli configuration). They will be serialized and stored in the document directory. The exact path will be printed in the console as “recoding to ...”.

Mocking Requests

let mockFloret = MockFloret(mode: .mock)

If you create your MockFloret in mock mode it will search for a MockFloret folder in the bundle. If you used the record mode you can just copy over the recorded MockFloret folder. Make sure to select “Create folder references” if you drag the folder to Xcode. For every request the MockFloret will then search in this folder if there is any recorded response. If there are multiple it will return a random response.

Manual Response Mapping

You can use the addMapping functions to manually map a request to a specific response.

// Maps all requests for the host cauli.works to the response
// stored in the MockFloret/default/foo path.
addMapping { request, mockFloret in
    guard request.url?.host == "cauli.works" else { return nil }
    return mockFloret.resultForPath("default/foo")
}

// Maps all requests for the url path /api/florets to
// a Not Found (404) response.
addMapping(forUrlPath: "/api/florets") { request, _ in
    Result<Response>.notFound(for: request)
}
  • Declaration

    Swift

    public var enabled: Bool
  • Declaration

    Swift

    public var description: String? { get }
  • Creates a new MockFloret instance and defines it’s mode.

    Declaration

    Swift

    public init(mode: Mode = .mock(forced: false))

    Parameters

    mode

    The Mode of this MockFloret.

  • Declaration

    Swift

    public func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)
  • Declaration

    Swift

    public func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)
  • This function will try to use the mocking storage (from within the bundle) to decode a Result by a given path.

    Declaration

    Swift

    public func resultForPath(_ path: String) -> Result<Response>?

    Parameters

    path

    The path of a record relative to the “MockFloret” folder.

    Return Value

    A Result or nil if no decodable Record at that path.

  • A MappingClosue maps a URLRequest to an optional Result.

    Declaration

    Swift

    public typealias MappingClosure = (URLRequest, MockFloret) -> Result<Response>?
  • Adds a manual mapping defining which Result to use for a certain Request.

    Declaration

    Swift

    @discardableResult
    public func addMapping(with closure: @escaping MappingClosure) -> Mapping

    Parameters

    closure

    A closure mapping a Request to a Result.

    Return Value

    The Mapping. Use this object to remove the mapping at a later time.

  • Adds a manual mapping defining which Result to use for a certain Request. The closure will only be applied if the path of the Requests url matches the given path.

    Declaration

    Swift

    @discardableResult
    public func addMapping(forUrlPath urlPath: String, with closure: @escaping MappingClosure) -> Mapping

    Parameters

    urlPath

    The expected path of the requests url.

    closure

    A closure mapping a Request to a Result.

    Return Value

    The Mapping. Use this object to remove the mapping at a later time.

  • Adds a manual mapping defining which Result to use for a certain Request. The closure will only be applied if the url of the Requests matches the given url.

    Declaration

    Swift

    @discardableResult
    public func addMapping(forUrl url: URL, with closure: @escaping MappingClosure) -> Mapping

    Parameters

    url

    The expected url of the Request.

    closure

    A closure mapping a Request to a Result.

    Return Value

    The Mapping. Use this object to remove the mapping at a later time.

  • Removes a manual Mapping. If the mapping is currently not configured it does nothing.

    Declaration

    Swift

    public func removeMapping(_ mapping: Mapping)

    Parameters

    mapping

    The mapping to remove.

  • All possible modes the MockFloret can be in.

    See more

    Declaration

    Swift

    public enum Mode : Equatable
  • A MockFloret.Mapping is an internal representation of a mapping from a URLRequest to a Result for a Response ((URLRequest, MockFloret) -> Result<Response>?). This Mapping will be returned when using the addMapping function and can be used to remove a Mapping via removeMapping.

    Declaration

    Swift

    public struct Mapping