5

I'm having issues unit testing code involving an isKindOfClass check. There are a lot of existing question on this topic and the answers recommend:

  • Make sure -ObjC is included as an "other linker flag". It is.
  • Make sure the .m files from the project target are not added to the test target. They aren't.
  • ...?

To make sure there weren't odd circumstances, I created a fresh project. I:

  • Created an "Empty Application" project
  • Added a PodFile referencing "TwistedOakCollapsingFutures"
  • Ran pod install
  • Opened the workspace
  • Verified -ObjC is present in both targets
  • Added a method returning [TOCFuture futureWithResult:@1] to AppDelegate.m
  • Added XCTAssert([AppDelegate.makeFuture isKindOfClass:TOCFuture.class], @"") to the example test
  • Ran the unit tests (the assert failed)

Basically I have no idea why this shouldn't work. Even more oddly, if I go counter to existing answers and include AppDelegate.m in the test target, the test starts to pass.

What is going on? Am I suppose to include the source files in the test target, or am I not supposed to?

Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
  • It has been seen before that, with multiple static libs included in an app, you can get more than one instance of the class object for a given class. – Hot Licks Aug 20 '14 at 11:52
  • (I believe what you need to do is to somehow reference the class in the "main" code, before either static lib references it.) – Hot Licks Aug 20 '14 at 11:54

1 Answers1

2

This answer solved it for me https://stackoverflow.com/a/27165120/2777364.

In short you should not link pod with your test target and you should create separate linking with at least one other pod for test target to force generating configuration set.

target 'MainTarget' do
    pod 'PodYouTryToTest'
end

target 'Tests' do
    pod 'AtLeastOneOtherPod'
end

Answer above is "The Right Way" of solving this. As a quick workaround I can propose a method:

Class getBundleDependentClass(Class class) { 
     return NSClassFromString(NSStringFromClass(class)); 
}
Community
  • 1
  • 1
Tomasz Bąk
  • 6,124
  • 3
  • 34
  • 48
  • What do you do with that method? Do you use it as a category? What does that look like in objc?? – TheJeff Sep 06 '16 at 23:04