我正在尝试为我的 iPhone 应用程序设置单元测试。我跟着苹果单元测试文档 http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html#//apple_ref/doc/uid/TP40007959-CH20-SW1通过并且工作得很好,但是当我在该测试中添加另一个类时,我收到以下错误:
"_OBJC_CLASS_$_RootViewController", referenced from:
__objc_classrefs__DATA@0 in AppDelegateTests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
该应用程序本身是一个基本的导航应用程序,具有用于数据存储的核心数据。
单元测试如下:
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "HSStabilityAppAppDelegate.h"
#import "RootViewController.h"
@interface AppDelegateTests : SenTestCase {
HSStabilityAppAppDelegate *appDelegate;
}
@end
@implementation AppDelegateTests
// all code under test must be linked into the Unit Test bundle
#pragma mark -
#pragma mark Set up and tearDown
#if APPLICATION_TESTS
- (void) setUp {
appDelegate = (HSStabilityAppAppDelegate *)[[UIApplication sharedApplication] delegate];
STAssertNotNil(appDelegate, @"Cannot find the application delegate.");
}
- (void) tearDown {
[appDelegate release];
}
#else
#endif
#pragma mark -
#pragma mark Tests
#if APPLICATION_TESTS
- (void) testRootViewIsOnTop {
id topViewControllerClass = [[appDelegate.navigationController topViewController] class];
id rootViewControllerClass = [RootViewController class];
STAssertEquals(topViewControllerClass, rootViewControllerClass, @"Root view controller was not the top class");
}
#endif
@end
如果我注释掉 id rootViewControllerClass 行,则程序链接正确。
另外,这种情况仅在针对设备目标构建时发生,如果针对模拟器构建,我不会遇到任何问题(可能考虑到应用程序测试在模拟器上不起作用)。
任何人都可以帮助解决这个基本且非常令人恼火的问题吗?
我也关注了Apple 的 iPhone 单元测试应用程序 http://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html#//apple_ref/doc/uid/TP40007959-CH20-SW3文档并在尝试对我的一个类进行单元测试时看到类似于问题中描述的链接错误。
看起来像单元测试类中引用的任何类,因此从测试目标运行也需要添加到该测试目标中。为此,您可以右键单击您的RootViewController
类并单击“获取信息”(Cmd-i 快捷键)。在目标窗格中,确保选中您的单元测试目标(例如“LogicTests”,如果您遵循该文档中的命名)。
现在该类将与您的测试一起编译,并且应该可用于您的单元测试。要仔细检查,请展开左侧“组和文件”浏览器中的“目标/逻辑测试/编译资源”节点。这列出了构建目标时可用的所有类文件,现在应该包括您的单元测试类以及正在测试的类。
(请注意,当您创建新应用程序或测试类时,您需要类似地选择所有适当的目标 - 当您命名文件时,在“新文件...”窗口的同一页面上)。
(顺便说一句,我正在使用 XCode 3.2.3 和 OS 4.0)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)