Originally created by @jshl8612 on GitHub (Apr 11, 2019).
Here is my test code in unit test
functestInsert(){// 1. ArrangeletdataStack:DataStack={letdataStack=DataStack(xcodeModelName:"Model")do{trydataStack.addStorageAndWait(InMemoryStore())}catchleterror{XCTFail("Cannot set up database storage: \(error)")}returndataStack}()// 2. Actiondo{trydataStack.perform(synchronous:{transactioninletobject=transaction.create(Into<Food>())object.name="Test"})}catchleterror{XCTFail("Cannot perform database transaction: \(error)")}// 3. Assertdo{trydataStack.perform(synchronous:{transactioninguardtrytransaction.fetchOne(From<Food>(),Where<Food>("name",isEqualTo:"Test"))!=nilelse{XCTFail("Cannot get database object")return}})}catchleterror{XCTFail("Cannot perform database transaction: \(error)")}}
It crashes on From.swift with a "Fatal error: Unexpectedly found nil while unwrapping an Optional value" log.
internalfuncapplyToFetchRequest<U>(_fetchRequest:CoreStoreFetchRequest<U>,context:NSManagedObjectContext,applyAffectedStores:Bool=true)throws{🚫fetchRequest.entity=context.parentStack!.entityDescription(for:EntityIdentifier(self.entityClass))!🚫guardapplyAffectedStoreselse{return}do{tryself.applyAffectedStoresForFetchedRequest(fetchRequest,context:context)}catchleterrorasCoreStoreError{CoreStore.log(error,"Attempted to perform a fetch but could not find any persistent store for the entity \(cs_typeName(fetchRequest.entityName))")throwerror}catch{throwerror}}
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
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)
letdataStack:DataStack={letdataStack=DataStack(xcodeModelName:"Model",bundle:Bundle(for:type(of:self))// or whatever Bundle your xcdatamodeld file is in)do{trydataStack.addStorageAndWait(InMemoryStore())}catchleterror{XCTFail("Cannot set up database storage: \(error)")}returndataStack}()
@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
}()
```
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)
letdataStack:DataStack={letdataStack=DataStack(xcodeModelName:"Model",bundle:Bundle(for:type(of:self))// or whatever Bundle your xcdatamodeld file is in)do{trydataStack.addStorageAndWait(InMemoryStore())}catchleterror{XCTFail("Cannot set up database storage: \(error)")}returndataStack}()
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~
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @jshl8612 on GitHub (Apr 11, 2019).
Here is my test code in unit test
It crashes on From.swift with a "Fatal error: Unexpectedly found nil while unwrapping an Optional value" log.
Could you help me out? Thanks~~
@JohnEstropia commented on GitHub (Apr 12, 2019):
For unit tests you'll need to pass in the
Bundlefor the unit test module as CoreStore doesn't know where to load it from (There is noBundle.main)@jshl8612 commented on GitHub (Apr 12, 2019):
Thanks for your answer, the issue is solved~