Xcode 测试未检测到我的课程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31335486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode Test not detect my class
提问by Imran Shamszadeh
I have class named Meal.swift in my project and a unit test
我的项目中有一个名为 Meal.swift 的类和一个单元测试
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"
但问题是:使用未解析的标识符“Meal”
采纳答案by Bart?omiej Semańczyk
回答by Mark Szymczyk
Xcode 7 adds the @testable import
statement to simplify user testing. At the top of your unit test class files, add the following statement:
Xcode 7 添加了该@testable import
语句以简化用户测试。在单元测试类文件的顶部,添加以下语句:
@testable import MyApp
Where MyAppis 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, MyAppin this example.
其中MyApp是您的 iOS 应用程序的名称。现在单元测试目标应该能够在您的应用程序中找到类并运行测试。如果您收到链接错误,提示 Xcode 找不到您的应用程序类,请确保产品模块名称构建设置的值与您在@testable import
语句中使用的名称相匹配,在本例中为MyApp。
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.
如果@testable import
不适合您,解决方法是使您的应用程序类成为单元测试目标的成员。您可以使用文件检查器在 Xcode 中设置文件的目标成员资格。
回答by ivyr
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.
然后在那时,它检测到我的 FoodTracker 类(Meal 类)并且错误消失了。
回答by Manu
Click on your Meal class. Then on the right side you'll see 'Target Membership' section. Select your test project. (Xcode 7) Voilà.
单击您的膳食课程。然后在右侧,您将看到“目标会员”部分。选择您的测试项目。(Xcode 7) 瞧。
回答by Firda Sahidi
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 Testtarget under class membershipof my new class. Or delete the class, make new class again, then select Unit Test Targetin that new class.
就我而言,我只在刚刚创建的新类中出错,这让我感到困惑。因此,它的工作原理是在我的新班级的班级成员下选择单元测试目标。或者删除类,重新创建新类,然后在该新类中选择单元测试目标。
回答by AmirHossein
Just add the class you are testing to Target-> BuildPhases-> Compile Sources
只需将您正在测试的类添加到Target-> Build Phases-> Compile Sources