Reputation: 626
I am trying to write a unit tests that uses a file in my project. In my application, I use FileManager.default.currentDirectoryPath
to get the path to my file. However, the test path is different when I use the same in tesing. After reading up the documentation I understood that XCode uses a different path for unit testing.
I was wondering how I can get the correct Bundle path for File.txt
in my tests
I have tried this but it returns null
let testBundle = Bundle(for: type(of: self))
let fileURL = testBundle.url(forResource: "File", withExtension: "txt")
The following is my directory structure. ShopTests is my unit test file and File.txt is the file for which I am trying to retrieve the path url for(both highlighted in picture)
Upvotes: 1
Views: 1626
Reputation: 11733
Here is how I am doing it in a test that works:
let dataURL = Bundle(for: TripBuilderTest.self).url(forResource: fileName, withExtension: "json")
I think I remember having had the same problem you are reporting. Try using classname.self as per above.
Upvotes: 3