Skip to content

Instantly share code, notes, and snippets.

@akolov
Created September 18, 2014 12:37
Show Gist options
  • Select an option

  • Save akolov/8894408226dab48d3021 to your computer and use it in GitHub Desktop.

Select an option

Save akolov/8894408226dab48d3021 to your computer and use it in GitHub Desktop.

Revisions

  1. akolov created this gist Sep 18, 2014.
    7 changes: 7 additions & 0 deletions TestCase.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class ExceptionTestCase: XCTestCase {
    func raisesException() {
    var exception = NSException(name: NSInternalInconsistencyException, reason: "Testing exceptions", userInfo: nil)
    XCTAssertThrows({ exception.raise() }, "Should raise an exception)
    XCTAssertThrowsSpecific({ exception.raise() }, NSInternalInconsistencyException, "Should raise NSInternalInconsistencyException")
    }
    }
    1 change: 1 addition & 0 deletions Tests-Bridging-Header.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #import "XCTestCase+Exceptions.h"
    9 changes: 9 additions & 0 deletions XCTestCase+Exceptions.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    @import Foundation;
    @import XCTest;

    @interface XCTestCase (Exceptions)

    - (void)XCTAssertThrows:(void (^)(void))block :(NSString *)message;
    - (void)XCTAssertThrowsSpecific:(void (^)(void))block :(NSString *)name :(NSString *)message;

    @end
    28 changes: 28 additions & 0 deletions XCTestCase+Exceptions.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #import "XCTestCase+Exceptions.h"

    @implementation XCTestCase (Exceptions)

    - (void)XCTAssertThrows:(void (^)(void))block :(NSString *)message {
    XCTAssertThrows(block(), @"%@", message);
    }

    - (void)XCTAssertThrowsSpecific:(void (^)(void))block :(NSString *)exceptionName :(NSString *)message {
    BOOL __didThrow = NO;
    @try {
    block();
    }
    @catch (NSException *exception) {
    __didThrow = YES;
    XCTAssertEqualObjects(exception.name, exceptionName, @"%@", message);
    }
    @catch (...) {
    __didThrow = YES;
    XCTFail(@"%@", message);
    }

    if (!__didThrow) {
    XCTFail(@"%@", message);
    }
    }

    @end