相关疑难解决方法(0)

应用源何时需要包含在测试目标中?

在一个新项目中,我有这个简单的测试

#import <XCTest/XCTest.h>
#import "ViewController.h"

@interface ViewControllerTests : XCTestCase
@end

@implementation ViewControllerTests

- (void)testExample
{ 
    // Using a class that is not in the test target.
    ViewController * viewController = [[ViewController alloc] init];
    XCTAssertNotNil(viewController, @"");
}

@end
Run Code Online (Sandbox Code Playgroud)

ViewController.h 不是测试目标的一部分,但它编译并运行测试没有任何问题.

在此输入图像描述

我认为这是因为应用程序首先构建(作为依赖)然后是测试.链接器然后找出ViewController类是什么.

但是,在较旧的项目中,使用完全相同的测试和ViewController文件,构建在链接器阶段失败:

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_ViewController", referenced from:
  objc-class-ref in ViewControllerTests.o
Run Code Online (Sandbox Code Playgroud)

即使创建了新的XCTest单元测试目标,也会发生此链接器错误.

为了解决这个问题,可以在应用程序和测试目标中包含源(勾选上图中的两个框).这会在模拟器的系统日志中导致重复符号的构建警告(打开模拟器并按cmd- /查看):

Class ViewController is implemented in both 
[...]/iPhone Simulator/ [...] /MyApp.app/MyApp and 
[...]/Debug-iphonesimulator/LogicTests.octest/LogicTests. 
One of the two will be used. Which one is undefined.
Run Code Online (Sandbox Code Playgroud)

这些警告偶尔会导致以下示例说明的问题: …

xcode unit-testing objective-c ios xctest

69
推荐指数
4
解决办法
2万
查看次数

标签 统计

ios ×1

objective-c ×1

unit-testing ×1

xcode ×1

xctest ×1