Found answer over here: https://forums.developer.apple.com/thread/11342
On Mon, Aug 22, 2016 at 2:41 PM, Devarshi Kulshreshtha < [email protected]> wrote: > I am making an app with name - "Medication", in which I am trying to > implement unit test cases for certain operations on my managed objects. > > -------------------- > Here is my entity: > > Entity name: Nurse > attribute 1: email > attribute 2: password > relationship: patient > > In model editor by default it is showing Class as 'Nurse' and Module as > 'Current Product Module' > > -------------------- > Here is - Nurse+CoreDataProperties.swift class > > import Foundation > import CoreData > > extension Nurse { > > @NSManaged var email: String? > @NSManaged var password: String? > @NSManaged var patient: NSSet? > > } > > -------------------- > Here is - Nurse.swift class > > class Nurse: NSManagedObject { > // adding nurse to local db > class func addNurse(withEmail email: String, password: String, > inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> > NSError? { > var insertError : NSError? = nil > > // Check if it is a duplicate entry > if isDuplicate(email: email, inManagedObjectContext: > managedObjectContext) { > // is duplicate > let userInfo: [NSObject : AnyObject] = > [NSLocalizedDescriptionKey : NSLocalizedString("Duplicate Nurse!", value: > "Nurse with same email already exists.", comment: "")] > insertError = NSError(domain: > CoreDataCustomErrorCodes.DuplicateRecord.domain, > code: CoreDataCustomErrorCodes.DuplicateRecord.rawValue, userInfo: > userInfo) > } > else { > // email does not exist > let newNurse = NSEntityDescription. > insertNewObjectForEntityForName(String(self), inManagedObjectContext: > managedObjectContext) as! Nurse > newNurse.email = email > newNurse.password = password > > do { > try managedObjectContext.save() > } catch { > let error = error as NSError > print("\(error), \(error.userInfo)") > insertError = error > } > } > > return insertError > } > } > > -------------------- > Here is - NurseTests.swift > > import XCTest > import CoreData > @testable import Medication > > class NurseTests: XCTestCase { > var managedObjectContext: NSManagedObjectContext? > > //MARK: Overriden methods > override func setUp() { > super.setUp() > // Put setup code here. This method is called before the > invocation of each test method in the class. > if managedObjectContext == nil { > managedObjectContext = setUpInMemoryManagedObjectContext() > } > } > > override func tearDown() { > // Put teardown code here. This method is called after the > invocation of each test method in the class. > super.tearDown() > } > > //MARK: Testing functions defined in Nurse.swift > // testing : class func addNurse(withEmail email: String, password: > String, inManagedObjectContext managedObjectContext: > NSManagedObjectContext) -> NSError? > func testAddNurse() { > let nurseEmail = "[email protected]" > let nursePassword = "clara" > > let error = Nurse.addNurse(withEmail: nurseEmail, password: > nursePassword, inManagedObjectContext: managedObjectContext!) > XCTAssertNil(error, "There should not be any error while adding a > nurse") > } > > } > > -------------------- > Problem is when I am trying to execute the test case it is showing me this > error in Xcode console: > > Could not cast value of type 'Medication.Nurse' (0x7ff69284a120) to > 'MedicationTests.Nurse' (0x111ed8388). > > And crashing at this line: > > let newNurse = > NSEntityDescription.insertNewObjectForEntityForName(String(self), > inManagedObjectContext: managedObjectContext) as! Nurse > > Any ideas guys? > > -- > Thanks, > > Devarshi > -- Thanks, Devarshi _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
