0

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

> Some more background information might be helpful @JanBrinker, i want to test some of the methods which return text, that contains localized text. currently its return localied key (screen_name_feature_text) becuase localized_text file path is not found so unit tests are failing
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.