Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't mock class method in Pod target #67

Closed
badeleux opened this issue Feb 7, 2014 · 4 comments
Closed

Can't mock class method in Pod target #67

badeleux opened this issue Feb 7, 2014 · 4 comments

Comments

@badeleux
Copy link

badeleux commented Feb 7, 2014

I am trying to mock static method in AFDownloadRequestOperation class (which is in Pods target)

id requestOperation = [OCMockObject mockForClass:[AFDownloadRequestOperation class]];
[[[[requestOperation stub] classMethod]andReturn:@"/Volumes/test5M"] cacheFolder];

In test file when I am trying to invoke this method, result is predictably correct, but when I am invoking this method in project target it turns out that method cacheFolder doesn't return @"/Volumes/test5M".

Is there any known issue that OCMock can't stub class methods in Pods target?

@erikdoe
Copy link
Owner

erikdoe commented Feb 11, 2014

I'm not aware of any issues. Are you saying everything compiles cleanly, the test gets invoked, the calls above do not throw any exceptions and still the cacheFolder message doesn't get stubbed? That would be really odd.

@badeleux
Copy link
Author

Ok, I've created example project to visualise this problem. Please clone this repo: https://github.com/badeleux/OCMockPodTest

My output for test method:

2014-02-14 08:27:01.406 OCMockPodTest[2820:70b] TEST STUB: /Volumes/test5M /Volumes/test5M
2014-02-14 08:27:11.008 OCMockPodTest[2820:70b] TEST STUB: /Volumes/test5M ~/Library/Application Support/iPhone Simulator/7.0.3/Applications/7E89CD46-D81B-45E0-96CA-B89C8F1B0383/tmp/Incomplete
2014-02-14 08:27:21.919 OCMockPodTest[2820:70b] TEST STUB: /Volumes/test5M /Volumes/test5M

This demo presents my problem. Stubbing mechanism works for ViewController which is in app target. Moreover stubbed method cacheFolder works in test file (OCMockPodTestTests.m:39 and 41) but no longer works in ViewController.m which is shown in line 40

@carllindberg
Copy link
Contributor

I suspect you have include the AFNetworking classes twice in your binary. I think both your library includes libPods.a, meaning it will have a copy of all of those classes. Your test bundle also includes libPods.a, meaning it will have another copy of all those classes. In Xcode 5, it appears that in this situation (rather than duplicate symbol errors), that two versions of each Class get added to the ObjC runtime with the same name. NSClassFromString (which OCMock uses) will only find the first one, probably, and only mock that class. Hard class references in one or the other product will refer to the version of the Class linked with that product -- so your library version will point to the Class linked into your library, whilst your test class code will refer to the one compiled into the test bundle. OCMock will only modify one or the other.

So... try removing libPods.a from the "link binary with libraries" build phase from either your library or the test bundle. They should either be provided bundled inside your library (in which case remove from the test case), or users should be expected to include them along with your library if they are using it, in which case remove libPods from your library target (and the test case, as a consumer of the library, would then need to include it).

@badeleux
Copy link
Author

Right. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants