1

Situation:

I have a document-based SwiftUI app using a DocumentGroup scene with a custom FileDocument.

struct ChessRoomApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: SomeDocument()) { file in
            ContentView(document: file.$document)
        }
    }
}

The problem:

In my XCUITests I would like to generate a document for each test and tear it down again after the test has completed. Currently I have a directory filled with test files stored in iCloud. I want to step away from that.

Where I got:

I managed to (or at least I think I managed to) create and teardown files in the app’s documents directory by basing myself on the example code here: developer.apple.com. This code gives me access to a temporary fileURL and looks like this:

func temporaryFileURL() -> URL {
    let fileManager = FileManager.default
    let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let filename = UUID().uuidString
    let fileURL = URL(fileURLWithPath: directory.absoluteString).appendingPathComponent(filename, conformingTo: .pgn)
        
    addTeardownBlock {
        do {
            if fileManager.fileExists(atPath: fileURL.path) {
                try fileManager.removeItem(at: fileURL)
                XCTAssertFalse(fileManager.fileExists(atPath: fileURL.path))
            }
        } catch {
            XCTFail("Error while deleting temporary file: \(error)")
        }
    }
        
    return fileURL
}

My question:

How do I load the file at that fileURL into my DocumentGroup scene to run my tests against?

Luïs
  • 2,671
  • 1
  • 20
  • 33

0 Answers0