导入 header 文件时单元测试失败

Unit test failing when importing a header file

我正在尝试在 Objective-C 的项目中实施单元测试。 问题是构建失败时我没有任何错误消息,经过几次测试后,我不知道如何执行构建。

- (void)setUp {
    [super setUp];

    self.order = [[TLSOrderEntity alloc] init];

    self.calculateVatDictionary = [[NSDictionary alloc] init];

    self.price100 = [NSNumber numberWithInteger:100];
    self.price200 = [NSNumber numberWithInteger:200];

    self.tax5 = [NSNumber numberWithFloat:5.5];
    self.tax10 = [NSNumber numberWithInteger:10];
    self.tax20 = [NSNumber numberWithInteger:20];
}

这个方法很好,可以构建,效果很好。当我尝试导入另一个 header 并尝试分配相应的 object 时,构建立即失败。

#import "TLSOrderLineEntity.h"

- (void)testExample {
    TLSOrderLineEntity *line1 = [TLSOrderLineEntity createEntityLineWithName:@"PRODUIT" price:self.price100 tax:self.tax10 extraLines:nil];
}

我还有其他接口可以无错误地获取私有方法,并声明一些属性:

#import <XCTest/XCTest.h>
#import <UIKit/UIKit.h>
#import "TLSOrderTaxManager.h"

@interface TLSOrderTaxManager (Test)

+ (NSDictionary *)calculateVatForLines:(TLSOrderEntity *)order;

@end

@interface TLSTaxTests : XCTestCase

@property (nonatomic, strong) TLSOrderEntity    *order;
@property (nonatomic, strong) TLSOrderLineEntity *orderLine;

@property (nonatomic, strong) NSDictionary      *calculateVatDictionary;

@property (nonatomic, strong) NSNumber          *price100;
@property (nonatomic, strong) NSNumber          *price200;

@property (nonatomic, strong) NSNumber          *tax5;
@property (nonatomic, strong) NSNumber          *tax10;
@property (nonatomic, strong) NSNumber          *tax20;

@end

正如我之前所说,我不知道为什么会失败。 是否存在依赖性问题?或者我应该编译相应的 .m 以包含它们?

感谢您的指教!

能否提供错误信息?尝试点击左侧面板中三角形内嵌感叹号的图标:

尝试按此面板底部的图标(它们负责 errors/warnings 过滤)。也许你之前已经禁用了错误。

根据你的问题描述,你似乎还没有将这个文件添加到测试目标中。尝试 select TLSOrderLineEntity.m 并检查此文件的测试目标:

正确配置的测试目标可以访问生产目标中的所有内容。

在测试目标的"Build Settings":

  • 为您的应用设置 "Test Host"。像 $(BUILT_PRODUCTS_DIR)/MyApp.app/MyApp
  • 将"Bundle Loader"设置为$(TEST_HOST)

在测试目标的 "General" 设置中:

  • 指定主机应用程序。确保选中复选框 "Allow testing Host Application APIs"。

如果您刚刚添加了新的生产目标 class 文件,并且如果您尝试导入它的 header(即使它的“.m”文件已添加到单元测试目标),它将失败因为 XCode 可能没有构建新 class 的 header 搜索路径。特别是如果您为新文件添加新目录。在这种情况下,只需清理项目即可解决问题。