问题
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
回答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")
来源:https://stackoverflow.com/questions/24049735/xcode-6-swift-and-mock-verification-without-exceptions