由于"nil"参数导致XCTestAssertNil崩溃

swe*_*py_ 16 xcode ios xctest swift

我正在使用XCTest在我的项目中编写单元测试,当使用XCAssertNil()XCAssertNotNil()方法时,XCTest框架崩溃了.

这是我的测试:

XCTAssertNotNil(messageCollection.fieldName, "field_name must be not-nil")
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪:

2015-06-22 17:05:17.629 xctest[745:8747] *** Assertion failure in void _XCTFailureHandler(XCTestCase *, BOOL, const char *, NSUInteger, NSString *, NSString *, ...)(), /SourceCache/XCTest_Sim/XCTest-7701/XCTestFramework/OtherSources/XCTestAssertionsImpl.m:41
Test Case '-[Wakanda_iOS_Framework_Tests.WAKAdapterTest testEntityCollectionParsing]' started.
2015-06-22 17:05:17.631 xctest[745:8747] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Parameter "test" must not be nil.'
Run Code Online (Sandbox Code Playgroud)

似乎XCTest有一个名为test的参数,它不能为零,对于预期检查nil(或非nil)值的方法来说很奇怪......有没有其他人得到这个问题并解决了它?

flo*_*ger 6

根据这个问题http://www.openradar.me/22409527,这似乎是XCTest中的一个错误,当你检查一个nil的可选项时会导致崩溃.

您可以使用以下方法修复测试:

XCTAssert(messageCollection.fieldName != nil, "field_name must be not-nil")
Run Code Online (Sandbox Code Playgroud)


Mac*_*nik 5

正如我在XCTestAssertionsImpl.h中看到的:

XCT_EXPORT void _XCTFailureHandler(XCTestCase *test, BOOL expected, const char *filePath, NSUInteger lineNumber, NSString *condition, NSString * __nullable format, ...) NS_FORMAT_FUNCTION(6,7);
Run Code Online (Sandbox Code Playgroud)

这是第一个参数-- test表示的实例XCTestCase。因此消息可能是:“ 嘿,您的XCTestCase对象不再存在,因此我无法在其上调用任何方法 ”。

例如,当您XCTAssert...在异步块中调用某些对象时,可能会发生这种情况,可能在封闭XCTestCase对象消失后很长时间才被调用。

如果在这种情况下[unowned self],将异步块添加到此处无法解决问题,则需要进行Expectations或同步代码。


Jon*_*eid 4

由于您的“测试”为零,我猜测您正在尝试XCAssertNil从您作为助手编写的独立函数进行调用。XCTest 断言self作为“测试”,因此它们不能位于独立函数中。它们必须在方法中。尝试将辅助函数更改为方法。