xcode 由于“nil”参数导致 XCTestAssertNil 崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30984061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:21:44  来源:igfitidea点击:

XCTestAssertNil crash due to "nil" parameter

iosxcodeswiftxctest

提问by sweepy_

I'm using XCTest to write unit tests in my project, and when using the XCAssertNil()or XCAssertNotNil()methods, XCTest framework crashes.

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

Here's my test:

这是我的测试:

XCTAssertNotNil(messageCollection.fieldName, "field_name must be not-nil")

Here's the stack trace:

这是堆栈跟踪:

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.'

It seems that XCTest has a parameter named testwhich cannot be nil, strange for a method expected to check for nil (or non-nil) values... Does anyone else got this problem and solved it?

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

回答by florieger

According to this rdar http://www.openradar.me/22409527, this seems to be a bug in XCTest, leading to a crash when you check an optional that is nil.

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

You can fix your test with:

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

XCTAssert(messageCollection.fieldName != nil, "field_name must be not-nil")

回答by Maciek Czarnik

As I see in XCTestAssertionsImpl.h:

正如我在 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);

It's first argument - test- refers to the instance of XCTestCase. So the message might be: "Hey man, your XCTestCaseobject doesn't exist any more, so I cannot invoke any method on it".

它的第一个参数 - test- 指的是XCTestCase. 所以消息可能是:“嘿,伙计,你的XCTestCase对象不再存在,所以我无法调用它的任何方法”。

It might be a case for example when you call some XCTAssert...in asynchronous block, that might be invoked long after your enclosing XCTestCaseobject has gone.

例如,当您XCTAssert...在异步块中调用 some时,可能会在封闭XCTestCase对象消失后很长时间内调用它。

If that might be a case adding [unowned self]to your async block wont solve the issue here, you need to go for Expectations or synchronise your code.

如果这可能是添加[unowned self]到您的异步块无法解决这里的问题的情况,您需要选择 Expectations 或同步您的代码。

回答by Jon Reid

Since your "test" is nil, I'm guessing you're trying to call XCAssertNilfrom a standalone function you wrote as a helper. The XCTest assertions take selfas "test", so they can't be in standalone functions. They must be in methods. Try changing your helper function to a method.

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

回答by basvk

Same error occured in a similar situation like this:

在类似的情况下发生了同样的错误:

let expectation = self.expectation(description: "exp")

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
    expectation.fulfill()
}

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
    XCTAssert(false)
}

waitForExpectations(timeout: 2, handler: nil)

So calling XCTAssertafter the expectation was fulfilled.
Which ofcourse is wrong.

所以XCTAssert在期望得到满足后调用。
这当然是错误的。

回答by pierre23

You can also get this error when the test function finished to execute but than if you have anything async that call any XCTAssert. It will crash your test. I had the same issue...

当测试函数完成执行时,您也可能会收到此错误,但如果您有任何异步调用任何 XCTAssert。它会使你的测试崩溃。我遇到过同样的问题...

回答by byedissident

If you are doing an Asynch you need to use a expectation:

如果你正在做一个异步你需要使用一个期望:

//PRIOR TO TEST<br>
let validation = expectation(description: "FullFill")
<br><br>
//AFTER ASYNCH IS COMPLETED<br>
validation.fulfill()

//BOTTOM OF YOUR TEST<br>
self.waitForExpectations(timeout: 10){ error in }