Xcode 6, Swift and mock verification without exceptions
Asked Answered
B

1

3

Anyone know how to verify an OCMock expect in Swift? Swift Doesn't use Exceptions so XCTest no longer includes XCTAssertNoThrow. Is There any other way to verify a method was called with OCMock? I noticed in OCMock the verify function checks the expectations array so I assume we could do XCTAssertTrue(mock.expectations == 0) if we had access.

Trying to figure some of these more complicated things out since there is little to no docs around XCTests in Swift

Bookmark answered 5/6, 2014 at 0:22 Comment(0)
B
1

Ok so Not a real answer but a work around for ObjC / Swift Projects. Write an Object Wrapper that will run [mock verify] and return a bool.

Create an Obj-c H and M file. In the .h

+ (BOOL)verifyMock:(id)mock;

In the .m

+ (BOOL)verifyMock:(id)mock
{
    BOOL called = YES;
    @try {
        [mock verify];
    }
    @catch (NSException *exception) {
        called = NO;
    }
    @finally {
        return called;
    }
    return called;
}

Now add this Obj-c file to your Bridging-Header file so swift has access.

In your Swift XCTest file

XCTAssertTrue(YourWrapperClassName.verifyMock(mock), "Method was not called")

Bookmark answered 5/6, 2014 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.