0
votes

We are having many development pods that are currently linked as dynamic frameworks. I'm trying to migrate the dynamic framework to static libraries For static-libs, I'm setting resource bundle like below (pod_spec)

s.resource_bundles = { 'BundleName' => [ 'BundleName/**/*.{xcassets,lproj,storyboard,xib,xcassets,imageset,png,mp3,mp4,wma}' ] }

and accessing in code like below

Bundle.main.url(forResource: "BundleName", withExtension: "bundle")

when I run the app, I'm getting the resource bundle path, from that I can able to get images/localized text

The same is not working when I run Unit tests. resource bundle path is nil and all my test related to localized text are failing

how can I fix this?

thanks in advance

2

2 Answers

1
votes

The issue is that your tests are a separate bundle from your application, so when running Bundle.main.url(...) it will try to give a resource from your test bundle, not your app bundle.

I guess the easiest way of doing things is to add the resources to your test bundle. Depending on what you want to achieve. Some more background information might be helpful.

0
votes

Answering my own question after struggled for a whole day

When the unit test runs your code, your unit test bundle is NOT the main bundle.

Even though you are running tests, not your application, your application bundle is still the main bundle. so, when you run unit tests with static libraries mode, you won't find resource_bundles if you search the main bundle.

you have to check in the list of Bundle and find the one with ".xctest" extension and append the module name

enter code here
class func bundle(for name: String) -> Bundle {
  var url = Bundle.main.bundleURL

  for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
    url = bundle.bundleURL.deletingLastPathComponent()
  }

  url = url.appendingPathComponent( name + "/" + name + ".bundle")

  guard let bundle = Bundle(url: url) else {
    return Bundle.main
  }

  return bundle
}