Git Product home page Git Product logo

mocking's Introduction

Mocking

Platforms Swift License

Introduction ๐Ÿ‘‹๐Ÿผ

A Swift package to add Mocking capabilities to your iOS app.

When mocking is enabled and registered, any URL will look for a corresponding JSON file by default that matches the sanitised value of the URL's absoluteString value.

Additional manual mocking can also be added to override this default behavour for specific needs.

Any URL that isn't mocked will be reported using OSLog to help idenfity areas to mock.

Prerequisites

  • Swift 5.7
  • iOS 14

Installation

Add the Mocking package as a dependency to your Package.swift file, and add it as a dependency to your target.

// swift-tools-version:5.7

import PackageDescription

let package = Package(
    name: "MyPackage",
    dependencies: [
        .package(url: "https://github.com/pdamonkey/Mocking.git", upToNextMajor: "1.0.0")
    ],
    targets: [
        .target(name: "MyPackage", dependencies: ["Mocking"])
    ]
)

Usage

To use the framework, import the module:

import Mocking

Check if the --mocking Launch Argument has been set:

if MockingURLProtocol.isMocking {
    ...
}

Register MockingURLProtocol with URLProtocol:

MockingURLProtocol.register()

Add MockingURLProtocol to the URL configuration if required:

configuration.protocolClasses = [MockingURLProtocol.self]

Add mock matching URL to return specific Data object and HTTP Response:

MockingURLProtocol.mock(url: url, data: data-to-return, response: response-to-return)

Add mock matching URL to return specific Data and optional Status Code (defaults to 200)

MockingURLProtocol.mock(url: url, data: data-to-return, 404)

Add mock matching URL to return specific Error and Status Code:

MockingURLProtocol.mock(url: url, error: MyError.notAuthenticated, statusCode: 401)

Use Regex to mock pretified URL's to a particular mock file:

MockingURLProtocol.mock(regex: "^my-url-thumbnail-*", data: Bundle.main.mockFile(name: "thumbnail"))

U&nregister MockingURLProtocol with URLProtocol (if required):

MockingURLProtocol.unregister()

mocking's People

Contributors

pdamonkey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.