1

i've looked over this for hours. I am using MagicalRecord. Everything is fine in the application , but in the unit tests i get a swift_dynamicCastClassUnconditional error when i run the following

func testExampleQCPerson() {

    let person  = QCPerson.MR_createEntity() as QCPerson //error happens here
    person.displayOrder = NSNumber(integer: 0)

    let personB  = QCPerson.MR_createEntity() as QCPerson
    personB.displayOrder = NSNumber(integer: 0)
    personB.updateOrderForPrevious()

    XCTAssert(person.displayOrder.integerValue == 1, "should have incremented")
}

as the past posts suggest, i need to make my models objective C friendly with @objc(ModelName)...so i have already done that:

import Foundation
import CoreData
@objc(QCPerson)
class QCPerson: NSManagedObject {

  @NSManaged var person_id: NSNumber
  @NSManaged var displayOrder: NSNumber
  @NSManaged var contactRefrence: String
  @NSManaged var name: String
  @NSManaged var actionType: String
}

while this makes the rest of the application functional, I'd really prefer to be able to test things as well. i will add, i can do this in the unit test

let xx: AnyObject = QCPerson.MR_createEntity() as AnyObject
XCTAssertNotNil(xx, "should exists")

but that doesn't help me with my class specific needs

mcconkiee
  • 21
  • 1
  • 2
  • Doing away with MagicalRecord for tests, and using standard "templated Core Data stack" - thanks to [this post](http://alanduncan.me/blog/2014/09/28/swift-core-data-tests/) i can at least test my models and their extensions – mcconkiee Jan 12 '15 at 04:53
  • Did you ever figure out how to make this work WITH MagicalRecord? – AnthonyMDev Feb 19 '15 at 02:07

1 Answers1

0

This was happening to me too, but I had a bit different scenario, since I am using ObjC managed object subclasses. Since Unit Tests in swift require some hacks to work, I added all the files to the test bundle too, but I should have omitted the ObjC ones, since they are imported through bridging header.

In your case, maybe it is due to the wrong class name specified in xcdatamodel: look at this Unable to find specific subclass of NSManagedObject

Community
  • 1
  • 1
Alessandro Orrù
  • 3,183
  • 16
  • 23