使用 XCode 4 进行单元测试的链接错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6461074/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:19:55  来源:igfitidea点击:

Linking error for unit testing with XCode 4?

objective-cxcodeunit-testinglinkerocunit

提问by shadowmatter

I want to write some logic unit tests for classes in my XCode application. In Xcode 4, I clicked on the project name in the Project Navigator, and from the bottom clicked Add Target. I chose "Cocoa Touch Unit Testing Bundle" under Other, give the new target a "product name" of "tests", and finish.

我想为我的 XCode 应用程序中的类编写一些逻辑单元测试。在 Xcode 4 中,我在 Project Navigator 中单击了项目名称,然后从底部单击了 Add Target。我在其他下选择了“Cocoa Touch Unit Testing Bundle”,给新目标一个“产品名称”“测试”,然后完成。

Because the class I want to test is compiled as part of my existing application target, for my new "tests" target I immediately go to the Build Phases tab and add my existing application target as the only target dependency.

因为我要测试的类是作为我现有应用程序目标的一部分编译的,所以对于我的新“测试”目标,我立即转到“构建阶段”选项卡并将我现有的应用程序目标添加为唯一的目标依赖项。

I then go to the created tests.m file, import the class I want to test (below it's ReleasePlanManager, and call one of its methods. But the linker fails with an error like:

然后我转到创建的 tests.m 文件,导入我想测试的类(在它下面ReleasePlanManager,并调用它的方法之一。但链接器失败并出现如下错误:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_ReleasePlanManager", referenced from:
      objc-class-ref in tests.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

So the class cannot be found, even though (from my understanding) adding the application target (which it is a part of) should be sufficient?

因此无法找到该类,即使(根据我的理解)添加应用程序目标(它是其中的一部分)应该就足够了?

Any help would be greatly appreciated. Thanks!

任何帮助将不胜感激。谢谢!

回答by Jon Reid

Your test bundle needs extra settings:

您的测试包需要额外的设置:

  • Set Bundle Loaderto $(BUILT_PRODUCTS_DIR)/AppName.app/AppName(replacing AppName in both places with your app's name)
  • Set Test Hostto $(BUNDLE_LOADER)
  • Bundle Loader设置为$(BUILT_PRODUCTS_DIR)/AppName.app/AppName(用您的应用程序名称替换两个地方的 AppName)
  • 测试主机设置为$(BUNDLE_LOADER)

(If you create a project from scratch and enable unit tests, these are set up for you. But if you add a unit test bundle to an existing project, they're not.)

(如果您从头开始创建一个项目并启用单元测试,这些是为您设置的。但是如果您将单元测试包添加到现有项目中,则它们不是。)