Imran Sh
Imran Sh

Reputation: 1774

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"

Upvotes: 30

Views: 18818

Answers (5)

Swift Dev Journal
Swift Dev Journal

Reputation: 20088

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.

Upvotes: 22

Firda Sahidi
Firda Sahidi

Reputation: 1281

In my case, I got error only in the new class I've just made, and it makes me confuse. So, it works by select Unit Test target under class membership of my new class. Or delete the class, make new class again, then select Unit Test Target in that new class.

Upvotes: 0

Manu
Manu

Reputation: 647

Click on your Meal class. Then on the right side you'll see 'Target Membership' section. Select your test project. (Xcode 7) Voilà.

Upvotes: 7

ivyr
ivyr

Reputation: 71

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.

Upvotes: 7

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

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

enter image description here

Upvotes: 25

Related Questions