Latest 0.1.1
Homepage https://github.com/mr-v/AssertThrows
License MIT
Platforms ios 8.0
Frameworks XCTest

Carthage compatible

Custom assertions for asserting throwing functions in Swift 2.x. For more information see blog post.

Usage

Custom assertions enable following throwing function assertions:

  1. Throws error.
  2. Throws specific ErrorType implementer.
  3. Throws specific case of ErrorType implementer.*

Sample usage:

func test_AssertsThrowingAnyError() {
    let movie = Movie()
    AssertThrows(try movie.throwOnUnkown(.Unkown))
}

func test_AssertsThrowingSpecificType() {
    let movie = Movie()
    AssertThrows(TestError.self, try movie.throwOnUnkown(.Unkown))
}

func test_AssertThrows_ThrowingSpecificCaseOfType() {
    let movie = Movie()
    AssertThrows(TestError.IllegalArgument, try movie.throwOnUnkown(.Unkown))
}

There’s one caveat – ErrorType conforming enumerations with associated data must also implement Equatable protocol to be verified with AssertThrows. Standard enums have it for free.

Custom assertions fix issues with vanilla XCTest tests that may have looked like this:

func test_throwOnUnkown_UnknownPassed_ThrowsAnyError() {
    do {
        try Movie().throwOnUnkown(.Unkown)
        XCTFail()
    } catch {
    }
}

func test_throwOnUnkown_UnknownPassed_ThrowsTestError() {
    do {
        try Movie().throwOnUnkown(.Unkown)
        XCTFail()
    } catch {
        guard let _ = error as? TestError else {
            return XCTFail()
        }
    }
}

func test_throwOnUnkown_UnknownPassed_ThrowsIllegalArgument() {
    do {
        try Movie().throwOnUnkown(.Unkown)
        XCTFail()
    } catch {
        guard let error = error as? TestError else {
            return XCTFail()
        }
        XCTAssertEqual(TestError.IllegalArgument, error)
    }
}

Installation

CocoaPods

In Podfile add AssertThrows to test target, e.g.

target 'AppTests' do
pod 'AssertThrows'
end

Carthage

Add to Cartfile:

github "mr-v/AssertThrows"

Drag the built .framework binary into your application’s test target.

Latest podspec

{
    "name": "AssertThrows",
    "version": "0.1.1",
    "summary": "Custom assertions for asserting throwing functions in Swift 2.x.",
    "description": "Fixes XCTest's missing support for testing error handling in Swift 2.x. Custom assertions enable following throwing function assertions:n1. Throws error.n2. Throws specific ErrorType implementer.n3. Throws specific case of ErrorType implementer.*",
    "homepage": "https://github.com/mr-v/AssertThrows",
    "license": {
        "type": "MIT",
        "file": "License"
    },
    "authors": "Witold Skibniewski",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/mr-v/AssertThrows.git",
        "tag": "0.1.1"
    },
    "source_files": "AssertThrows/AssertThrows.swift",
    "frameworks": "XCTest",
    "pod_target_xcconfig": {
        "ENABLE_BITCODE": "NO"
    }
}

Pin It on Pinterest

Share This