OCUnit测试在未测试的类中给出错误 [英] OCUnit Tests giving errors in classes not being tested

查看:122
本文介绍了OCUnit测试在未测试的类中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个OCUnit测试,该测试在Xcode 3.2中运行良好,现在在Xcode 4中遇到了问题.它在类A中测试单个静态函数X,该函数不会调用任何类或函数(库函数之外).唯一需要调用的其他代码是静态构造函数,该构造函数将初始化两个静态变量,但同样,这些是不会调用任何其他类或类A的函数的硬编码值(数组).

但是,我必须为它导入的所有类包括.m,.mm和.c文件,并导入它们,依此类推,以解决 _OBJC_CLASS _ $ _ ClassB",从:错误.我不记得必须在Xcode 3中执行任何操作,但是很好,没问题,我可以对其进行编译.现在,我收到了源自类B+[B initialize].

为什么在没有以任何方式引用的类上调用静态构造函数?我该如何解决此问题,最好在不更改班级以适应测试的情况下?

更新

为了进一步了解发生了什么,我注释掉了所有测试用例和#import "A.h"以查看会发生什么.我添加了一个简单的单元测试:

- (void) testSomething {
    STAssertTrue(NO, @"did it work?");
}

仍然,我的+[B initialize]被调用并失败.似乎OCUnit正在搜寻我所有的类,并且在过程中调用了它们的+initialize方法.这对我来说毫无意义-如何禁用此行为?

这是调用我自己的代码之前的堆栈跟踪信息,以防万一:

#7  _class_initialize ()
#8  prepareForMethodLookup ()
#9  lookUpMethod ()
#10 objc_msgSend ()
#11 +[NSObject(SenTestRuntimeUtilities) senIsASuperclassOfClass:] ()
#12 +[NSObject(SenTestRuntimeUtilities) senAllSubclasses] ()
#13 +[SenTestSuite updateCache] ()
#14 +[SenTestSuite suiteForBundleCache] ()
#15 +[SenTestSuite testSuiteForBundlePath:] ()
#16 +[SenTestProbe specifiedTestSuite] ()
#17 +[SenTestProbe runTests:] ()
#18 <????> ()
#19 <????> ()
#20 <????> ()
#21 <????> ()

解决方案

我在xcode 4和单元测试套件中也遇到了许多问题.

我必须要看您的消息源才能知道您的情况,但是OCUnit通过对类的反思确实做了很多事情.这就是它找出运行以"test"开头的方法的方式.因此从某种意义上讲,它正在检查您的类,从而导致其类初始化程序被触发.

我知道这可能不是您要寻找的答案,但是如果B的+ initialize错误是因为它依赖于某种预期的应用程序状态,则您可能真的想考虑重构+ initialize.出于这个确切的原因,它不应该依赖于类之外的任何东西……您无法保证它何时会触发或事物处于什么状态.

I have written an OCUnit test, which ran fine in Xcode 3.2, and now I'm having problems with it in Xcode 4. It tests a single static function X in class A, which doesn't call any classes or functions (outside of library functions). The only other code that should need to be called is the static constructor, which initializes two static variables, but again, those are hardcode values (arrays) that don't call any other classes, or functions of class A.

And yet, I had to include the .m, .mm, and .c files for all classes that it imports, and that they import, and so on, to solve the _OBJC_CLASS_$_ClassB", referenced from: errors. I don't recall having to do any of that in Xcode 3, but fine, no problem, I got it to compile. Now, I'm getting errors originating from class B's +[B initialize].

Why is the static constructor getting called on a class that is not referenced in any way? How can I fix this, ideally without changing my classes to acommodate the testing?

Update

To further figure out what's going on, I commented out all my test cases and the #import "A.h" to see what would happen. I added a single simple unit test:

- (void) testSomething {
    STAssertTrue(NO, @"did it work?");
}

Still, my +[B initialize] gets called and fails. It seems that OCUnit is crawling through all of my classes, and their +initialize methods are getting called in the process. This makes no sense to me - how can I disable this behavior?

Here is the stack trace before my own code gets called, in case it helps:

#7  _class_initialize ()
#8  prepareForMethodLookup ()
#9  lookUpMethod ()
#10 objc_msgSend ()
#11 +[NSObject(SenTestRuntimeUtilities) senIsASuperclassOfClass:] ()
#12 +[NSObject(SenTestRuntimeUtilities) senAllSubclasses] ()
#13 +[SenTestSuite updateCache] ()
#14 +[SenTestSuite suiteForBundleCache] ()
#15 +[SenTestSuite testSuiteForBundlePath:] ()
#16 +[SenTestProbe specifiedTestSuite] ()
#17 +[SenTestProbe runTests:] ()
#18 <????> ()
#19 <????> ()
#20 <????> ()
#21 <????> ()

解决方案

I had a number of problems with xcode 4 and my unit test suites as well.

I'd have to see your source to know what's going on in your case, but OCUnit does do a lot of stuff through reflection on classes. That's how it figures out to run methods that start with "test". So it makes some sense that it's inspecting your classes, causing their class initializers to fire.

I know this probably isn't the answer you're looking for, but if the errors in B's +initialize are because it's relying on some sort of expected application state, you may really want to consider refactoring +initialize. It shouldn't be dependent on anything outside the class itself for this exact reason... you have no guarantees on when it will fire or what state things will be in.

这篇关于OCUnit测试在未测试的类中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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