Issues with SwiftData @Relationships in Swift Testing

Is it a known issue that when attempting to test SwiftData objects with relationships in Swift Testing there are errors that do not occur when the app is running?

Example:

@Relationship(deleteRule: .cascade, inverse: \MBAccount.book) var accounts: [MBAccount] = []

and

@Relationship var book: Microbook?

Then the test:

        let book = Microbook(name: "Julia's Cupcakes")
        let account1 = MBAccount(name: "Chase Business Account")
        let account2 = MBAccount(name: "Cash Drawer")
        account1.book = book
        account2.book = book
        #expect(account1.name == "Chase Business Account")
        #expect(account2.type == "asset")
    }

Produces a fatal error while running test:

SwiftData/PersistentModel.swift:321: Fatal error: Unable to find relationship on MBAccount for KeyPath \MBAccount.book

Any ideas?

Answered by Developer Tools Engineer in 792349022

Hi @daviddotto! Thanks for letting us know about this problem. It is not a known issue. Would you please file feedback with Apple using Feedback Assistant? Thanks!

Hi @daviddotto! Thanks for letting us know about this problem. It is not a known issue. Would you please file feedback with Apple using Feedback Assistant? Thanks!

Issues with SwiftData @Relationships in Swift Testing
 
 
Q