Localizations not loading in XCTest?

I have a circuit in Xcode that only has a test configuration. And this diagram points to a goal that has all my tests (this is Cocoa's target installation of an object test block for OS X).

So, I am doing Command + U to run all of these tests, and it works fine. However, several tests require access to localization resources. I added these resources to my goal and even checked the localization resources in the resulting binary binary.

However, the code does not see the resources, so when any test executes an NSLocalizedString, it returns a key instead of a localized string.

Is there anything special I need to do so that tests can see these resources?

+8
xcode localization macos xctest
source share
1 answer

The date of publication on this subject is a bit older, but I ran into this problem. I found this wonderful blog post that described this problem and has a great solution.

http://drekka.ghost.io/the-state-of-xcode-unit-testing/#xctestunittestsbug1incorrectbundleapplebug15306667

It has two solutions: the first (doesn't work for me):

replace

[NSBundle mainBundle] 

from

 [NSBundle bundleForClass:[self class]] 

The second solution (works fine, which uses OCMock):

 static id _mockNSBundle; +(void)setUp { _mockNSBundle = [OCMockObject niceMockForClass:[NSBundle class]]; NSBundle *correctMainBundle = [NSBundle bundleForClass:[self class]]; [[[[_mockNSBundle stub] classMethod] andReturn:correctMainBundle] mainBundle]; } +(void)tearDown { [_mockNSBundle stopMocking]; _mockNSBundle = nil; } 

Hope this helps people who check out this post in the future.

+5
source share

All Articles