简体   繁体   English

Xcode 测试未检测到我的课程

[英]Xcode Test not detect my class

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”

@testable import MyApp should work fine. @testable import MyApp应该可以正常工作。 Just remember to set appropriate configurations within Debug for your UITest target.请记住在Debug为您的UITest目标设置适当的配置。

在此处输入图片说明

Xcode 7 adds the @testable import statement to simplify unit testing. Xcode 7 添加了@testable import语句以简化单元测试。 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.其中MyApp是您的 iOS 应用程序的名称。 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.如果您收到链接错误,指出 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.如果@testable import对您不起作用,则解决方法是使您的应用程序类成为单元测试目标的成员。 You can set the target membership of a file in Xcode using the file inspector.您可以使用文件检查器在 Xcode 中设置文件的目标成员资格。

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 类)并且错误消失了。

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) 瞧。

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.或者删除类,重新创建新类,然后在该新类中选择单元测试目标

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”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM