【问题标题】:Imported files not recognized in OCUnitOCUnit 无法识别导入的文件
【发布时间】:2010-07-01 17:23:59
【问题描述】:

我正在使用 XCode 3.2.3 和 iOS 4.0 上的 OCUnit 对我的 iPhone 应用程序进行单元测试。我已成功设置测试环境以适当地通过和失败基本测试,但是当我导入自己的文件(在本例中为“UserAccount.h”时,它无法编译并告诉我:

“_OBJC_CLASS_$_UserAccount”,引用自:

然后显示“未找到符号”。这让我觉得是某种链接器错误,但我不知道发生了什么。我已经多次构建和清理所有目标,但无济于事。这是我的测试代码:

#import "SomeTestCase.h"
#import "UserAccount.h"

@implementation SomeTestCase

 - (void)testUserAccount
 {
 // UserAccount.m //

 UserAccount *testAccount = [[UserAccount alloc] initWithUsername:@"" password:@"" deviceToken:@""];
 [testAccount registerNew];
 NSLog(@"USERID = %@", testAccount.userID);
 STAssertEquals([testAccount login], NO, @"Failure: Login should fail with blank username and password."); // should fail with no username or password

 UserAccount *testAccount2 = [[UserAccount alloc] initWithUsername:@"user" password:@"" deviceToken:@""]; 
 STAssertEquals([testAccount2 login], NO, @"Failure: Login should fail with blank password.");// should fail with no password

 UserAccount *testAccount3 = [[UserAccount alloc] initWithUsername:@"" password:@"pass" deviceToken:@""]; 
 STAssertEquals([testAccount3 login], NO, @"Failure: Login should fail with blank username.");// should fail with no password

 }

@end

任何建议将不胜感激。 谢谢!

-马特

【问题讨论】:

    标签: iphone unit-testing xcode linker ocunit


    【解决方案1】:

    至少在 XCode 4 中,您应该将您的应用程序 .m 文件包含在您的 test 目标中。正确的做法是:

    1. 您的项目(左上角)-> 目标 -> 您的测试目标 -> 目标依赖项 -> + 您的主应用目标
    2. 切换到构建设置选项卡 -> 链接 -> Bundle Loader -> $(BUILT_PRODUCTS_DIR)/YourAppName.app/YourAppName

    查看如何正确完成此操作的一个好方法是创建一个带有单元测试的全新 XCode 4 项目,然后查看测试目标是如何设置的。您会注意到应用程序 .m 文件未包含在测试目标中。

    【讨论】:

      【解决方案2】:

      我猜 UserAccount.m 没有被添加到测试目标中。这将导致“找不到符号”错误。我见过多个目标,即使实现文件不是目标的一部分,Xcode 也会识别标头。

      如果这不起作用,请尝试使用 Xcode>Empty Caches... 清空 Xcode 缓存。

      【讨论】:

        猜你喜欢
        • 2021-04-01
        • 1970-01-01
        • 2022-10-17
        • 2016-12-25
        • 1970-01-01
        • 2020-12-17
        • 2023-03-31
        • 2021-04-23
        • 2017-09-05
        相关资源
        最近更新 更多