Test Library for Swift's Error Handling
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CatchingFire.xcodeproj
src
test
xcconfigs @ bd51f1f
.gitignore
.gitmodules
CatchingFire.podspec
LICENSE
README.md

README.md

CatchingFire

License: MIT CocoaPods

CatchingFire is a Swift test framework, which helps making expectations against the error handling of your code. It provides for this purpose two higher-order functions, which take throwing functions and check whether the given closure throws or not. It integrates seamlessly with the expecters provided by XCTest.

Usage

AssertNoThrow

AssertNoThrow allows you to write safe tests for the happy path of failable functions. It helps you to avoid the try! operator in tests.

If you want to test a function, which may fail in general, you may think of using try. But this would mean that you have to declare your test method as throwing, which causes that XCTest doesn't execute the test anymore.

So in consequence, you would usually need to write:

XCTAssertEqual(try! fib(x), 21)

If the expression fails, your whole test suite doesn't execute further and aborts immediately, which is very undesirable, especially on CI, but also for your workflow when you use TDD.

Instead you can write now:

AssertNoThrow {
    XCTAssertEqual(try fib(x), 21)
}

Or alternatively:

AssertNoThrow(try fib(x)).map { (y: Int) in
    XCTAssertEqual(y, 21)
}

If the expression fails, your test fails.

AssertThrow

AssertThrow allows to easily write exhaustive tests for the exception paths of failable functions. It helps you to avoid writing the same boilerplate code over and over again for tests.

If you want to test a function, that it fails for given arguments, you would usually need to write:

do {
    try fib(-1)
    XCTFail("Expected to fail, but did not failed!")
} catch Error.ArgumentMayNotBeNegative {
    // succeed silently
} catch error {
    XCTFail("Failed with a different error than expected!")
}

Instead you can write now:

AssertThrow(Error.ArgumentMayNotBeNegative) {
    try fib(-1)
}

If the expression or closure doesn't throw the expected error, your test fails.

Installation

CatchingFire is available through CocoaPods. To install it, simply add it to your test target in your Podfile:

use_frameworks!
target "AppTest" do
  pod 'CatchingFire'
end

Author

Marius Rackwitz, git@mariusrackwitz.de
Find me on Twitter as @mrackwitz.

License

Version is available under the MIT license. See the LICENSE file for more info.