Daily iOS
Testing

Testing Core Data and SwiftData

How do you set up unit tests for a store layer that depends on Core Data (or SwiftData) so that nothing is left on disk?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Have the store take an NSPersistentContainer or ModelContainer from outside, and in tests inject a memory-only container built with NSInMemoryStoreType, a /dev/null URL, or isStoredInMemoryOnly for SwiftData. Verify migrations separately against a real SQLite file.

Share a question