title: XCTest only: No NSEntityDescriptions in any model claim the NSManagedObject subclass
body: <p><strong>Let me outline the relevant code:</strong></p>
  <p>I have a DataManager class as follows:</p>
  <pre><code>enum DataManagerType {
      case normal, preview, testing
  }
  
  class DataManager: NSObject, ObservableObject {
  
      static let shared = DataManager(type: .normal)
      static let preview = DataManager(type: .preview)
      static let testing = DataManager(type: .testing)
  
      @Published var todos = [Todo]()
  
      fileprivate var managedObjectContext: NSManagedObjectContext
      private let todosFRC: NSFetchedResultsController&lt;TodoMO&gt;
  
      private init(type: DataManagerType) {
          switch type {
          case .normal:
              let persistentStore = PersistentStore()
              self.managedObjectContext = persistentStore.context
          case .preview:
              let persistentStore = PersistentStore(inMemory: true)
              self.managedObjectContext = persistentStore.context
              for i in 0..&lt;10 {
                  let newTodo = TodoMO(context: managedObjectContext)
                  newTodo.title = &quot;Todo \(i)&quot;
                  newTodo.isComplete = false
                  newTodo.date = Date()
                  newTodo.id = UUID()
              }
              try? self.managedObjectContext.save()
          case .testing:
              let persistentStore = PersistentStore(inMemory: true)
              self.managedObjectContext = persistentStore.context
          }
  
          let todoFR: NSFetchRequest&lt;TodoMO&gt; = TodoMO.fetchRequest()
          todoFR.sortDescriptors = [NSSortDescriptor(key: &quot;date&quot;, ascending: false)]
          todosFRC = NSFetchedResultsController(fetchRequest: todoFR,
                                                managedObjectContext: managedObjectContext,
                                                sectionNameKeyPath: nil,
                                                cacheName: nil)
          super.init()
  
          // Initial fetch to populate todos array
          todosFRC.delegate = self
          try? todosFRC.performFetch()
          if let newTodos = todosFRC.fetchedObjects {
              self.todos = newTodos.map({todo(from: $0)})
          }
      }
  
      func saveData() {
          if managedObjectContext.hasChanges {
              do {
                  try managedObjectContext.save()
              } catch let error as NSError {
                  NSLog(&quot;Unresolved error saving context: \(error), \(error.userInfo)&quot;)
              }
          }
      }
  
      private func fetchFirst&lt;T: NSManagedObject&gt;(_ objectType: T.Type, predicate: NSPredicate?) -&gt; Result&lt;T?, Error&gt; {
          let request = objectType.fetchRequest()
          request.predicate = predicate
          request.fetchLimit = 1
          do {
              let result = try managedObjectContext.fetch(request) as? [T]
              return .success(result?.first)
          } catch {
              return .failure(error)
          }
      }
  }
  </code></pre>
  <p>My persistence store is as such:</p>
  <pre><code>struct PersistentStore {
  
      let container: NSPersistentContainer
  
      init(inMemory: Bool = false) {
          container = NSPersistentContainer(name: &quot;CoreDataModel&quot;)
          if inMemory {
              container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: &quot;/dev/null&quot;)
          }
          container.viewContext.automaticallyMergesChangesFromParent = true
          container.loadPersistentStores(completionHandler: { (storeDescription, error) in
              if let error = error as NSError? {
                  fatalError(&quot;Unresolved error \(error), \(error.userInfo)&quot;)
              }
          })
      }
  
      var context: NSManagedObjectContext { container.viewContext }
  
      func saveContext () {
          if context.hasChanges {
              do {
                  try context.save()
              } catch let error as NSError {
                  NSLog(&quot;Unresolved error saving context: \(error), \(error.userInfo)&quot;)
              }
          }
      }
  }
  </code></pre>
  <p>I get an error when calling the following in an XCTest:</p>
  <pre><code>let predicate = NSPredicate(format: &quot;id = %@&quot;, todo.id as CVarArg) //todo.id is just some UUID() //irrelevant here
  let result = fetchFirst(TodoMO.self, predicate: predicate)
  </code></pre>
  <p><strong>This is the error I get:</strong></p>
  <blockquote>
  <p>2022-07-09 21:36:17.425709-0400 CoreDataExample[73965:7495035] [error]
  error: No NSEntityDescriptions in any model claim the NSManagedObject
  subclass 'CoreDataExampleTests.TodoMO' so +entity is confused. Have
  you loaded your NSManagedObjectModel yet ? CoreData: error:
  No
  NSEntityDescriptions in any model claim the NSManagedObject subclass
  'CoreDataExampleTests.TodoMO' so +entity is confused. Have you loaded
  your NSManagedObjectModel yet ?
  2022-07-09 21:36:17.425780-0400 CoreDataExample[73965:7495035] [error] error:
  +[CoreDataExampleTests.TodoMO entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
  CoreData: error: +[CoreDataExampleTests.TodoMO entity] Failed to find a unique
  match for an NSEntityDescription to a managed object subclass
  /Users/santiagogarciasantos/Documents/Xcode
  Projects/CoreDataExample/CoreDataExample/DataManager/DataManager.swift:87:
  error: -[CoreDataExampleTests.CoreDataExampleTests test_Add_Todo] :
  executeFetchRequest:error: A fetch request must have an entity.
  (NSInvalidArgumentException)</p>
  </blockquote>
  <p>Following other solutions on here, I've checked the target, made sure my Entity is in &quot;Current Product Module&quot; but it still won't work.</p>
  <p><strong>Important: This only occurs when I'm in using XCTest (using my DataManager.testing)</strong>, not in Previews or in simulator</p>
  <p>Here's a link to a <a href="https://github.com/santi-g-s/CoreDataExample" rel="nofollow noreferrer">demo project</a> I've made which recreates the issue.</p>
  <p>Thanks for your help!</p>