Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test internals of an Objective-C framework with Swift

I have a framework/module written in Objective-C and want to test its external interfaces and internals in Swift. For that I created a new test target in the same project, targeting the framework. In the Swift code I can import the module and use the public classes without a problem. However, I can't access the internal classes in my tests.

I tried the following:

  • I set the visibility of the internal sources to "Project". For what I understood that should make them available to code in the same project. It doesn't work, though. Or do I need to import them differently then?
  • I also tried the new @testable import feature of Swift 2 in Xcode 7, but that doesn't seem to work (yet) for Objective-C frameworks.

What I came up with is to manually import all privat sources I want to test in the bridging header of the test target. That works. However, it doesn't feel like it's the intended way of testing an Objective-C framework with Swift.

Any ideas on how to do that better?

like image 575
Frank Schlegel Avatar asked Jun 22 '15 15:06

Frank Schlegel


1 Answers

The way I have handled such issues in the past is to make public classes within the framework that subclass your internal classes that you want to test. You can add compiler flags to make them only built with debug or test configurations.

For instance, if you had a class called SomeClass, you would have SomeClassTest that would override all the methods publicly. Doing this also gives you the handy ability to easily include test logic, such as inserting dummy data where needed etc.

like image 191
rikola Avatar answered Sep 23 '22 09:09

rikola