XCode 4的单元测试是否链接错误? [英] Linking error for unit testing with XCode 4?
问题描述
我想为我的XCode应用程序中的类编写一些逻辑单元测试.在Xcode 4中,我在项目浏览器"中单击了项目名称,然后从底部单击了添加目标".我在其他"下选择可可接触单元测试包",为新目标指定测试"的产品名称",然后完成.
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.
因为我要测试的类已编译为现有应用程序目标的一部分,所以对于新的测试"目标,我立即转到构建阶段"选项卡,并将现有应用程序目标添加为唯一的目标依赖项.
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.
然后转到创建的tests.m文件,导入要测试的类(在ReleasePlanManager
下面,并调用其方法之一.但是链接器失败,并显示以下错误:
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:
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!
推荐答案
您的测试包需要额外的设置:
Your test bundle needs extra settings:
- 将捆绑程序加载器设置为
$(BUILT_PRODUCTS_DIR)/AppName.app/AppName
(在两个位置都将AppName替换为您的应用名称) - 将测试主机设置为
$(BUNDLE_LOADER)
- Set Bundle Loader to
$(BUILT_PRODUCTS_DIR)/AppName.app/AppName
(replacing AppName in both places with your app's name) - Set Test Host to
$(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.)
这篇关于XCode 4的单元测试是否链接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!