OCUnit Tests giving errors in classes not being tested

元气小坏坏 提交于 2019-12-24 03:41:33

问题


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 <????> ()

回答1:


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.




回答2:


DougW is correct, that I'm doing things in my +initialize method that probably shouldn't be in there. In any case, I wasn't ready to refactor that only so my unit tests would run.

I used the code below to skip that code, which wasn't required by my unit tests in the first place. I hope this helps someone else out there, as it would be useful in any function, not just the static initializer.

+ (void)initialize
{
    if (NSClassFromString(@"SenTestSuite")) {
        NSLog(@"Not initializing 'B' class, because unit tests are running");
        return;
    }

    ...
}


来源:https://stackoverflow.com/questions/5332348/ocunit-tests-giving-errors-in-classes-not-being-tested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!