OCMock Failing at runtime
Asked Answered
E

1

5

I'm trying to use OCMock for the first time in my test cases. It's a Mac project, built on and targeting Lion, in Xcode 4.3. The main app and the test bundle both have ARC turned on, and so every time I execute the tests I see the following log message:

GC: forcing GC OFF because OBJC_DISABLE_GC is set

That's fine, as I'm using ARC so I don't care about GC. When I build my unit tests, linked against the latest stable release of OCMock (2.0.1), the build has no issues. At runtime, after the above log statement, I get the following:

The test bundle at /Users/___/Library/Developer/Xcode/DerivedData/___-ayizwpehemunvodsdvczckkvarsh/Build/Products/Debug/___Tests.octest could not be loaded because its Objective-C runtime information does not match the runtime information required by the test rig. This is likely because the test rig is being run with Objective-C garbage collection disabled, but the test bundle requires Objective-C garbage collection. To enable Objective-C garbage collection for the test rig, run it in an environment without the OBJC_DISABLE_GC environment variable. 2012-03-06 10:29:32.812 otest[8486:203] *** NSTask: Task create for path '/Users/___/Library/Developer/Xcode/DerivedData/___-ayizwpehemunvodsdvczckkvarsh/Build/Products/Debug/___Tests.octest/Contents/MacOS/___Tests' failed: 22, "Invalid argument". Terminating temporary process.

The message implies that garbage collection is the most common culprit, but as mentioned, there is no way I'm using GC. So, what other settings could be messing me up at runtime? I didn't think I'm doing anything atypical, and I've looked through my test project's settings to be sure, and didn't see anything weird.

Update

I was able to reproduce this with a new empty project.

  1. Create a new project and have it create unit tests, with ARC enabled
  2. Clear the Test Host setting from the unit test bundle's build settings
  3. Link to the OCMock framework
  4. Execute the tests, and witness the same error I reported above

Also, when I turn off ARC and make garbage collection Required, then clang reports a mach-o linker error, so the build doesn't succeed. If I remove the link to the OCMock framework, it builds fine. This supports my initial thought that the problem lies somewhere other than garbage collection.

Ember answered 6/3, 2012 at 16:25 Comment(2)
It says it's likely not working because GC is OFF. "the test bundle requires Objective-C garbage collection". So try not setting OBJC_DISABLE_GC.Beep
@Beep the "test bundle" is my own test code, and it uses ARC, which is why GC is disabled. That message is just a hint at what is usually wrong in the runtime environment. I tried enabling GC, but LLVM fails, probably because ARC and GC can't coexist.Ember
E
18

I found the answer, after a day of searching everything I could think of, in the Hamcrest tutorial (reading more carefully through the OCMock tutorial linked from OCMock.org, it's mentioned there, too). For some reason, Hamcrest's and OCMock's frameworks need to be copied to the products directory. Then everything works like a charm.

  1. Go to the settings for your Tests Bundle
  2. Go to the Build Phases tab
  3. Click the Add Build Phase button, and select Add Copy Files
  4. Set the new Copy Files build phases' Destination to Products Directory
  5. Drag OCMock.framework from the project outline into the list of files for the phase
  6. Drag the Copy Files phase above the Run Script phase

Execute your tests as usual

Ember answered 7/3, 2012 at 0:53 Comment(3)
Both OCHamrest and OCMock are built with @rpath in the loader path. This makes them work in the products directory because that is on the path by default. You can use LC_RPATH and other means to give you more flexibility. See the discussion of @rpath on the dyld man page.Nullify
I had the same problem with my project, which includes the Growl framework. The solution was the same, thank you!Registrar
Wow, that was a hard one to track-down. Many thanks for posting this.Commines

© 2022 - 2024 — McMap. All rights reserved.