I am writing some integration tests and I want to load a file as a Data object. I keep getting the error "couldn't be opened because there is no such file". This is with the file in the same folder as the test class. I also tried creating a new asset catalog into the same project as the test but it also does not work.
Here's a few attempts that did not work.
let fileUrl = URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)media.png")
let mediaFile = try Data(contentsOf: fileUrl)
let fileUrl = URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent("media.png")
let mediaFile = try Data(contentsOf: fileUrl)
let bundle = Bundle(for: TheNameOfYourXCTestCaseSubClass.self)
Using the class name of your XCTestCase
-derived class the above code gives you the corresponding Bundle.
Afterwards use the methods on Bundle
to access the specific file.