我正在嘗試爲我的iPhone應用程序設置單元測試。我跟着Apple Unit Testing documentation通過和woked罰款,但只要我在給測試增加了一個班,我得到以下錯誤:iPhone單元測試:調用自定義代碼時找不到符號
"_OBJC_CLASS_$_RootViewController", referenced from:
[email protected] 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線,則程序的鏈接。另外,只有在針對設備目標進行構建時纔會發生這種情況,如果在模擬器上構建應用程序(可能因爲應用程序測試不能在模擬器上運行),則不存在任何問題。
任何人都可以幫助解決這個基本和非常令人生氣的問題嗎?
您需要確保您添加的類的源代碼包含在您的測試目標中。在目標> {your_test_target}>編譯源代碼下,您應該看到您的類的實現。例如。如果您有「#import MyClass.h」,請確保您的測試目標的「編譯源代碼」下有「MyClass.m」。 – 2010-03-16 14:53:10