【问题标题】:iPhone unit testing: Symbols not found when calling custom codeiPhone单元测试:调用自定义代码时找不到符号
【发布时间】:2023-03-05 02:05:01
【问题描述】:

我正在尝试为我的 iPhone 应用程序设置单元测试。我遵循Apple Unit Testing documentation 并唤醒了它,但是一旦我在该测试中添加了另一个类,我就会收到以下错误:

  "_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 行,则程序链接正确。 此外,这只发生在针对设备目标构建时,如果针对模拟器构建我没有任何问题(可能是因为应用程序测试在模拟器上不起作用)。

任何人都可以帮助解决这个基本且非常令人恼火的问题吗?

【问题讨论】:

    标签: iphone unit-testing symbols


    【解决方案1】:

    我还关注了Apple's iPhone Unit Testing Applications 文档,并在尝试对我的一个类进行单元测试时看到了类似于问题中描述的链接错误。

    看起来像您的单元测试类中引用的任何类,因此从您的测试目标运行也需要添加到该测试目标。为此,您可以右键单击 RootViewController 类并单击“获取信息”(Cmd-i 快捷方式)。在目标窗格上,确保选中您的单元测试目标(例如“LogicTests”,如果您遵循该文档中的命名)。

    现在该类将与您的测试一起编译,并且应该可用于您的单元测试。要仔细检查,请在左侧的“组和文件”浏览器中展开“目标/逻辑测试/编译资源”节点。这列出了构建目标时可用的所有类文件,现在应该包括您的单元测试类和您的测试类。

    (请注意,当您创建新应用程序或测试类时,您需要类似地选择所有适当的目标 - 命名文件时在“新建文件...”窗口的同一页面上)。

    (顺便说一下,我使用的是 XCode 3.2.3 和 OS 4.0)。

    【讨论】:

      【解决方案2】:

      我相信我已经找到了答案。问题是,虽然编译器可以看到定义,但在将它们链接起来时并没有在正确的位置查找,因此会引发这些错误。因此,如果我们将类名解析移到运行时,我们可以绕过这一切。

      代替:NSManagedObject 使用:NSClassFromString("@NSManagedObject")

      这几乎适用于其中定义的所有类。

      如果有人能告诉我如何让它在编译时工作,我仍然非常感激。

      【讨论】:

      • 您需要确保您添加的类的源代码包含在您的测试目标中。在 Targets>{your_test_target}>Compile Sources 下,您应该会看到类的实现。例如。如果您有“#import MyClass.h”,请确保您的测试目标在“编译源”下有“MyClass.m”。
      猜你喜欢
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      相关资源
      最近更新 更多