Use resources in unit tests with Swift Package Manager

前端 未结 7 487
孤街浪徒
孤街浪徒 2020-12-08 21:12

I\'m trying to use a resource file in unit tests and access it with Bundle.path, but it returns nil.

This call in MyProjectTests.swift returns nil:

7条回答
  •  Happy的楠姐
    2020-12-08 21:26

    I found another solution looking at this file.

    It's possible to create a bundle with a path, for example:

    let currentBundle = Bundle.allBundles.filter() { $0.bundlePath.hasSuffix(".xctest") }.first!
    let realBundle = Bundle(path: "\(currentBundle.bundlePath)/../../../../Tests/MyProjectTests/Resources")
    

    It's a bit ugly, but if you want to avoid a Makefile, it works.

提交回复
热议问题