How to make unit test case #263

Closed
opened 2025-12-29 15:27:50 +01:00 by adam · 2 comments
Owner

Originally created by @jshl8612 on GitHub (Apr 11, 2019).

Here is my test code in unit test

    func testInsert() {
        // 1. Arrange
        let dataStack: DataStack = {
            let dataStack = DataStack(xcodeModelName: "Model")
            do {
                try dataStack.addStorageAndWait(InMemoryStore())
            } catch let error {
                XCTFail("Cannot set up database storage: \(error)")
            }
            return dataStack
        }()
        
        // 2. Action
        do {
            try dataStack.perform(synchronous: { transaction in
                let object = transaction.create(Into<Food>())
                object.name = "Test"
        
            })
        } catch let error {
            XCTFail("Cannot perform database transaction: \(error)")
        }
        
        // 3. Assert
        do {
            try dataStack.perform(synchronous: { transaction in
                guard try transaction.fetchOne(From<Food>(), Where<Food>("name", isEqualTo: "Test")) != nil else {
                    XCTFail("Cannot get database object")
                    return
                }
            })
        } catch let error {
            XCTFail("Cannot perform database transaction: \(error)")
        }
    }

It crashes on From.swift with a "Fatal error: Unexpectedly found nil while unwrapping an Optional value" log.

    internal func applyToFetchRequest<U>(_ fetchRequest: CoreStoreFetchRequest<U>, context: NSManagedObjectContext, applyAffectedStores: Bool = true) throws {
        
        🚫fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!🚫
        guard applyAffectedStores else {
            
            return
        }
        do {

            try self.applyAffectedStoresForFetchedRequest(fetchRequest, context: context)
        }
        catch let error as CoreStoreError {

            CoreStore.log(
                error,
                "Attempted to perform a fetch but could not find any persistent store for the entity \(cs_typeName(fetchRequest.entityName))"
            )
            throw error
        }
        catch {

            throw error
        }
    }

Could you help me out? Thanks~~

Originally created by @jshl8612 on GitHub (Apr 11, 2019). Here is my test code in unit test ```swift func testInsert() { // 1. Arrange let dataStack: DataStack = { let dataStack = DataStack(xcodeModelName: "Model") do { try dataStack.addStorageAndWait(InMemoryStore()) } catch let error { XCTFail("Cannot set up database storage: \(error)") } return dataStack }() // 2. Action do { try dataStack.perform(synchronous: { transaction in let object = transaction.create(Into<Food>()) object.name = "Test" }) } catch let error { XCTFail("Cannot perform database transaction: \(error)") } // 3. Assert do { try dataStack.perform(synchronous: { transaction in guard try transaction.fetchOne(From<Food>(), Where<Food>("name", isEqualTo: "Test")) != nil else { XCTFail("Cannot get database object") return } }) } catch let error { XCTFail("Cannot perform database transaction: \(error)") } } ``` It crashes on From.swift with a "Fatal error: Unexpectedly found nil while unwrapping an Optional value" log. ```swift internal func applyToFetchRequest<U>(_ fetchRequest: CoreStoreFetchRequest<U>, context: NSManagedObjectContext, applyAffectedStores: Bool = true) throws { 🚫fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!🚫 guard applyAffectedStores else { return } do { try self.applyAffectedStoresForFetchedRequest(fetchRequest, context: context) } catch let error as CoreStoreError { CoreStore.log( error, "Attempted to perform a fetch but could not find any persistent store for the entity \(cs_typeName(fetchRequest.entityName))" ) throw error } catch { throw error } } ``` Could you help me out? Thanks~~
adam added the question label 2025-12-29 15:27:50 +01:00
adam closed this issue 2025-12-29 15:27:50 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Apr 12, 2019):

For unit tests you'll need to pass in the Bundle for the unit test module as CoreStore doesn't know where to load it from (There is no Bundle.main)

        let dataStack: DataStack = {
            let dataStack = DataStack(
                xcodeModelName: "Model",
                bundle: Bundle(for: type(of: self)) // or whatever Bundle your xcdatamodeld file is in
            )
            do {
                try dataStack.addStorageAndWait(InMemoryStore())
            } catch let error {
                XCTFail("Cannot set up database storage: \(error)")
            }
            return dataStack
        }()
@JohnEstropia commented on GitHub (Apr 12, 2019): For unit tests you'll need to pass in the `Bundle` for the unit test module as CoreStore doesn't know where to load it from (There is no `Bundle.main`) ```swift let dataStack: DataStack = { let dataStack = DataStack( xcodeModelName: "Model", bundle: Bundle(for: type(of: self)) // or whatever Bundle your xcdatamodeld file is in ) do { try dataStack.addStorageAndWait(InMemoryStore()) } catch let error { XCTFail("Cannot set up database storage: \(error)") } return dataStack }() ```
Author
Owner

@jshl8612 commented on GitHub (Apr 12, 2019):

For unit tests you'll need to pass in the Bundle for the unit test module as CoreStore doesn't know where to load it from (There is no Bundle.main)

        let dataStack: DataStack = {
            let dataStack = DataStack(
                xcodeModelName: "Model",
                bundle: Bundle(for: type(of: self)) // or whatever Bundle your xcdatamodeld file is in
            )
            do {
                try dataStack.addStorageAndWait(InMemoryStore())
            } catch let error {
                XCTFail("Cannot set up database storage: \(error)")
            }
            return dataStack
        }()

Thanks for your answer, the issue is solved~

@jshl8612 commented on GitHub (Apr 12, 2019): > For unit tests you'll need to pass in the `Bundle` for the unit test module as CoreStore doesn't know where to load it from (There is no `Bundle.main`) > > ```swift > let dataStack: DataStack = { > let dataStack = DataStack( > xcodeModelName: "Model", > bundle: Bundle(for: type(of: self)) // or whatever Bundle your xcdatamodeld file is in > ) > do { > try dataStack.addStorageAndWait(InMemoryStore()) > } catch let error { > XCTFail("Cannot set up database storage: \(error)") > } > return dataStack > }() > ``` Thanks for your answer, the issue is solved~
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#263