Search code examples
iosunit-testingbundlexcode8nslocalizedstring

Xcode 8 Ambiguous expansion of macro NSLocalizedString


I have the following in my unit test .pch file to allow my unit tests to find the right bundle for the localization files and this was working fine until I upgraded to Xcode8.

#undef NSLocalizedString
#define NSLocalizedString(key, comment) [[NSBundle bundleWithIdentifier:@"Tests-Unit"] localizedStringForKey:(key) value:@"" table:nil]

Now, I get the warning

Ambiguous expansion of macro NSLocalizedString 

Which would explain why my unit tests fail as they can't find the localized string value anymore.

Any ideas what is wrong there? Is there something I need to now do differently?

EDIT: I have a feeling it has something to do with all my source file being in 2 targets i.e one for the project and one for the unit tests which is the way we had it setup. I'm trying to clean this up and removed all my source file from the unit test target and added the

@testable import ProjectName

instead to a Unit test file to be able to access my code for testing but i'm now getting

File 'MyFile.swift' is part of module 'ProjectName'; ignoring import.

Solution

  • I eventually got it all working again.

    Let’s pretend our project is called Panda and it consists of both Obj-C and Swift files. All of those files are in both our Panda and PandaTests target.

    STEP 1: Ensure all your files have the right Target Membership i.e. Panda target: Only the source files, development frameworks, images etc and PandaTests target: Only the test files, testing frameworks, mock data etc

    STEP 2: Ensure your Panda Project has the Build Settings -> Enable Testability set to Yes.

    STEP 3: Ensure your PandaTests Project has the Build Settings -> Product Module Name set to PandaTests.

    STEP 4: Do a nice clean by holding down the Option button and then clicking on Product. You should see a Clean Build Folder option.

    STEP 5: For Swift Unit Tests, add @testable import Panda. Since we have now removed all our source code files from the PandaTests target, the unit tests need a way of accessing our project files. This enables our Swift unit tests to access all our Swift files and those Obj-C files that have been included in our Panda-Bridging-Header.h.