XCTestAssertNil cannot be used with Swift struct
| Originator: | alex | ||
| Number: | rdar://18110715 | Date Originated: | 23-Aug-2014 |
| Status: | Duplicate/18063644 | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6 beta-6 |
| Classification: | Bug | Reproducible: | Always |
Summary:
XCTestAssertNil cannot be used to check Swift structs to not be nil because Swift structs do not conform to AnyObject
Steps to Reproduce:
1. Create a new Xcode Swift project that uses unit tests
2. Create a new test case
3. In the test case declare a new struct like the following: struct StructA {}
4. In a test method create a new instance of StructA
5. Try to assert that the instance is not nil using XCTAssertNil
Expected Results:
The test should pass because the instance is not nil
Actual Results:
The compiler fails because XCTAssertNil expects an auto closure returning an instance of AnyObject? but StructA does not conform to AnyObject
Version:
Xcode 6.0 (6A280e)
Notes:
Changing the return type of the auto closure from AnyObject? to Any? should fix the problem
One can also declare a custom version of XCTAssertNil that takes an auto closure returning Any? as the first argument and does exactly the job. A version like this should be included in the XCTest Framework:
func XCTAssertNil(expression: @autoclosure () -> Any?, _ message: String? = nil) {
if let message = message {
XCTAssert(expression() == nil, message)
} else {
XCTAssert(expression() == nil)
}
}
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!