Call Objective-C class from Swift test file

I am working on unit tests. The original project was written in Objective-C.

I created a new test target written in Swift.

How can I call the Objective-C class of the actual application in a test file?

I tried to do the following.

@testable import MyModule

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

I tried several other things with project settings, but none of them worked.

Did I miss something obviously 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.
    }
}
+4
source share
1 answer

Objective-C. YourProductName-Bridging-Header.h, import HomeViewController .

Objective-C Swift

Objective-C Objective-C, Swift. :

"XYZCustomCell.h"

"XYZCustomView.h"

import "XYZCustomViewController.h" , Swift Compiler - Code Generation, , Objective-C Bridging Header

. , , Info.plist . .

, .

+3

Source: https://habr.com/ru/post/1652102/


All Articles