Recently I pointed out a better way to test optionals using XCTAssertNotNil. So as I’ve been writing a lot more tests recently, I decided to try this way out. But I soon ran into a problem:

The error message here is “‘PROJECTNAME’ does not implement methodSignatureForSelector: — trouble ahead”. After wrecking my head for a while, I came across this blogpost.

Turns out, this is happening because my Minion class is NOT a subclass of NSObject. Once, I imported Foundation and subclasses my Minion from NSObject, the problem disappeared:

XCTAssertNotNil also doesn’t work if I choose to make the Minion a struct instead of a class:

I’m not a fan of adding extra code to my classes (or making something a class vs a struct) for the sake of testing, so I think I’ll be switching back to the unwrapping my optionals in tests at least for my own classes that do not derive from NSObject…

Looking forward to Apple improving XCTest to work better with Swift!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.