Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Test not detect my class

I have class named Meal.swift in my project and a unit test

func testMealInitialization() {
    // Success case.
    let potentialItem = Meal(name: "Newest meal", photo: nil, rating: 5)
    XCTAssertNotNil(potentialItem)

    // Failure cases.
    let noName = Meal(name: "", photo: nil, rating: 0)
    XCTAssertNil(noName, "Empty name is invalid")

}

But the problem is that: Use of unresolved identifier "Meal"

like image 794
Imran Sh Avatar asked Jul 10 '15 07:07

Imran Sh


3 Answers

@testable import MyApp should work fine. Just remember to set appropriate configurations within Debug for your UITest target.

enter image description here

like image 57
Bartłomiej Semańczyk Avatar answered Jan 22 '23 23:01

Bartłomiej Semańczyk


Xcode 7 adds the @testable import statement to simplify unit testing. At the top of your unit test class files, add the following statement:

@testable import MyApp

Where MyApp is the name of your iOS app. Now the unit test target should be able to find the classes in your app and run the tests. If you get link errors saying that Xcode cannot find your app classes, make sure the Product Module Name build setting's value matches the name you use in the @testable import statement, MyApp in this example.

If @testable import does not work for you, a workaround is to make your app classes members of the unit test target. You can set the target membership of a file in Xcode using the file inspector.

like image 30
Swift Dev Journal Avatar answered Jan 23 '23 00:01

Swift Dev Journal


I also encountered this issue, what worked for me is reloading my test file and retyping the

@testable import FoodTracker

then at that point, it detected my FoodTracker classes (Meal class) and errors are gone.

like image 21
ivyr Avatar answered Jan 23 '23 01:01

ivyr