访问是从单元测试模拟类变量静态变量 [英] Accessing static variables that are simulating class variables from unit tests

查看:329
本文介绍了访问是从单元测试模拟类变量静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Objective-C运行时库函数(的不会的)或一组能够在Objective-C检测静态的(准一流水平)变量的函数?我知道我可以使用一个类访问方法,但我希望能够在不写我的code为测试框架进行测试。

Is there an Objective-C runtime library function (unlikely) or set of functions capable of inspecting static (quasi-class level) variables in Objective-C? I know I can utilize a class accessor method but I'd like to be able to test without writing my code "for the test framework".

或者的,有一个不起眼的普通的C技术的外部访问静态瓦尔?请注意,此信息对单元测试的目的,它不一定是适合生产使用。我意识到this'd违背静态瓦尔的意图......同事开始讨论这个话题,我在钻研ObjC / C内部总是很感兴趣。

Or, is there a obscure plain C technique for external access to static vars? Note this information is for unit testing purposes—it needn't be suitable for production use. I'm conscious that this'd go against the intent of static vars... a colleague broached this topic and I'm always interested in digging into ObjC/C internals.

@interface Foo : NSObject
+ (void)doSomething;
@end

@implementation Foo
static BOOL bar;
+ (void)doSomething
{
  //do something with bar
}
@end

鉴于以上情况,我可以使用运行时库或其他C接口检查?静态变量是一个C结构,也许还有内存特定区域为静态瓦尔?我感兴趣的是可模拟类变量在ObjC和可测试以及其他结构。

Given the above can I use the runtime library or other C interface to inspect bar? Static variables are a C construct, perhaps there's specific zone of memory for static vars? I'm interested in other constructs that may simulate class variables in ObjC and can be tested as well.

推荐答案

没有,真的没有,除非你是暴露了通过某些类方法或其他静电变量。你可以提供一个 +(BOOL)validateBar 方法,它不检查任何您需要,然后调用从你的测试框架。

No, not really, unless you are exposing that static variable via some class method or other. You could provide a + (BOOL)validateBar method which does whatever checking you require and then call that from your test framework.

另外,是不是一个Objective-C的变量,而是一个C变量,所以我怀疑是在Objective-C运行任何可以帮助。

Also that isn't an Objective-C variable, but rather a C variable, so I doubt there is anything in the Objective-C Runtime that can help.

这篇关于访问是从单元测试模拟类变量静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆