Giter VIP home page Giter VIP logo

errorassertions's Introduction

ErrorAssertions

tests Documentation Version Swift Package Manager

Wrappers for Swift assertions that take Error instances instead of Strings, and a suite of test functions to test these assertions.

Getting Started

To use ErrorAssertions, simply import it at the top of your Swift source file:

import ErrorAssertions

Just by doing this, since the Swift compiler will prefer imported modules to the main Swift module, you’ll get the ErrorAssertion versions of functions like fatalError(_:file:line:).

Using Error Types

To use an Error instead of a String when calling an assertion method, use the error version:

import ErrorAssertions

doSomething(completionHandler: { error in
    if let error = error {
        fatalError(error)
    }
})

You can use Error types with fatalError(), assert(), assertionFailure(), precondition(), and preconditionFailure().

Testing Assertions

In your tests, import the ErrorAssertionExpectations module to test assertions made in your app (as long as you’ve imported ErrorAssertions). In your test cases, use the expectation methods:

func testThatAnErrorHappens() {
    expectFatalError {
        doAThingThatProducesAFatalError()
    }
}

There are also versions that take an Error or String and validate that the produced error is the one you’re expecting:

func testThatASpecificErrorHappens() {
    expectFatalError(expectedError: URLError.badURL) {
        loadURL("thisisnotaurl")
    }
}

Installation

Swift Package Manager

Swift Package Manager is the preferred way to install ErrorAssertions. Add the repository as a dependency:

dependencies: [
    .package(url: "https://github.com/SlaunchaMan/ErrorAssertions.git",
             from: "0.2.0")
]

In your targets, add ErrorAssertions as a dependency of your main target and, if you’re using the test support, add ErrorAssertionExpectations to the test target:

targets: [
    .target(name: "App", dependencies: ["ErrorAssertions")]
    .testTarget(name: "AppTests", dependencies: ["ErrorAssertionExpectations"])
]

CocoaPods

To use ErrorAssertions with CocoaPods, use the main pod as a dependency in your app and the ErrorAssertionExpectations pod in your tests:

target 'App' do
    pod 'ErrorAssertions'
end

target 'AppTests' do
    pod 'ErrorAssertionExpectatoins'
end

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.