UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Error testing Core Data -> Could not cast value of type MyApp.ContactStorage as MyAppTests.ContactStorage

Forums > Swift

Hello folks.

I'm following the Ultimate Portfolio App videos, and when I get to the part of "Testing Core Data" video, I'm getting a very strange error and I cannot understand why.

The failure When I get to the part of:

  func testDeletingProjectCascadeDeletesItems() throws {
        try dataController.createSampleData()

        let request = NSFetchRequest<ContactStorage>(entityName: "ContactStorage")
        let contacts = try managedObjectContext.fetch(request)

The code is almost the same to the original tutorial (except the entity's name, and the class name.

The fetch request fails, and the output error is "Could not cast value of type MyApp.ContactStorage as MyAppTests.ContactStorage"

I think it's something related to the request or the fetch method, but I have no clue of what it is.

There's a previous test that passes, which includes the "try dataController.createSampleData()"

func testSampleDataCreationWorks() throws {
        try dataController.createSampleData()

        XCTAssertEqual(dataController.count(for: ContactStorage.fetchRequest()), 6, "There should be 6 sample projects.")
        XCTAssertEqual(dataController.count(for: NoteStorage.fetchRequest()), 50, "There should be 50 sample items.")
    }

Can anybody point me towards a solution?

The whole code is here: https://github.com/LeTarrask/Dualogue

Thanks.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.