Could not cast value of type 'Medication.Nurse' to 'MedicationTests.Nurse'
Could not cast value of type 'Medication.Nurse' to 'MedicationTests.Nurse'
- Subject: Could not cast value of type 'Medication.Nurse' to 'MedicationTests.Nurse'
- From: Devarshi Kulshreshtha <email@hidden>
- Date: Mon, 22 Aug 2016 14:41:56 +0530
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@hidden"
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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden