OGeek|极客世界-中国程序员成长平台

标题: ios - Objective C - 如何测试私有(private)变量 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:58
标题: ios - Objective C - 如何测试私有(private)变量

如何对隐藏变量 aVar 进行单元测试?

// .h file    
@interface Class: NSObject

@end

// .m file    
@implementation Class{
 id aVar
}

@end



Best Answer-推荐答案


您可以将私有(private)变量移动到 class extension ,这使它们保持私有(private),但随后创建一个使它们公开的 -Private 类别。例如。

// Class.h
@interface Class : NSObject
@end

// Class.m
@interface Class ()
@property (nonatomic, strong) id aVar;
@end

@implementation Class
@end

// Class+Private.h
@interface Class (Private)
@property (nonatomic, strong) id aVar;
@end

...

然后在您的单元测试目标中导入/编译 Class+Private。

关于ios - Objective C - 如何测试私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31975663/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4