6

I'm working on implementing unit tests. The original project was written in Objective-C.

I created a new Test Target that is written in Swift.

How do I call an Objective-C class of the actual app in my test file?

I've tried doing the following.

@testable import MyModule

However, this seems to only work if all the files are in Swift, which is not the case for me.

I've tried several other things with the project settings however, none of these seemed to work.

Am I missing something blatantly obvious?

class MyTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        let vc = HomeViewController() //this line is failing. How do I expose this view controller written in objective c?
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }
}
shim
  • 9,289
  • 12
  • 69
  • 108
prawn
  • 2,643
  • 2
  • 33
  • 49

1 Answers1

8

You need to create bridging header and add Objective-C file in to that. Create a bridging header YourProductName-Bridging-Header.h and then import HomeViewController in bridging header.

To import Objective-C code into Swift from the same target

In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:

#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h" 

In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting under has a path to the bridging header file. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.

Below are some resources which can help you

  1. Using Swift with Objective-C
  2. Answer to another stack overflow qustion
  3. How to use Objective-C in swift
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13423890) – shim Aug 22 '16 at 20:10
  • Make sense, will copy them across – Adnan Aftab Aug 22 '16 at 20:12
  • So theoretically, if I wanted to test every class, every view controller....I would have to include every header file into that bridging header file? – prawn Aug 22 '16 at 20:27
  • Yes if u are importing objective-c code to swift you have to do, or u can create a header file in objective c which import all classes and then import that header file inside bridging header, which technically is same as u import all in bridging header. – Adnan Aftab Aug 22 '16 at 20:44