Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTest under different locale

Tags:

xcode

xctest

Is it possible to test the outcome of a test under a different device locale?

I'd like to test method that is using

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

To determine the current device language. Now in my XCTest I'd like to check what happens when I'm in e.g."en-gb" instead of just "en", or simply test for a completely different language.

like image 790
Hons Avatar asked Apr 29 '16 07:04

Hons


1 Answers

The NSLocalizedString methods load the appropriate strings for the keys on launch for fast lookup, so you can't shift it around after launching. What you can do is run a test suite with one locale, then run the same test suite in a different locale.

You can set the locale for an app run in the Scheme Editor by selecting the Run action, Options tab, and picking Application Language and Application Region. Both default to the System settings, but you can change them, including using some convenient fake RTL and fake double-length languages.

(If the Test action doesn't inherit those settings for whatever reason, then there are also commandline arguments you can set to alter the defaults lookup used to order language and region preferences. This might be desirable for scripting purposes anyway.)

like image 134
Jeremy W. Sherman Avatar answered Oct 14 '22 22:10

Jeremy W. Sherman