vendredi 27 mars 2015

iOS - Writing Core Data Unit Tests in Swift

Recently I have been trying to convert the majority of my code in a project to swift. Things were going fairly smoothly until I hit a number of unit tests that tested the applications Core Data functionalities. Here is the test in question, I have a number of questions regarding issues that I have encountered while trying to run it.



import UIKit
import XCTest
import CoreData

class CalculateConversationSinceTimeTest: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testCalculateSinceTime() {
let managedObjectModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle.mainBundle()])!

let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil, error: nil)

let managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

let calculateConversationSinceTime = CalculateConversationSinceTime(managedObjectContext: managedObjectContext, messageRetentionPeriod: 9000000, bareJidStr: "test")
}
}


This unit test tests a swift class called CalculateConversationSinceTime (not a view controller), and as it stands all it does is creates an instance of the class. My first question is regarding the setup of the managedObjectModel, persistentStoreCoordinator and the managedObjectContext. I have never done this in a swift unit test and would like some validation that its being done correctly. Secondly, I am seeing a number errors/warnings inside the testCalculateSinceTime() and can't seem to figure out why. First off I get an error that says that CalculateConversationSinceTime does not have a member named init when I try to initialize a new instance. The initializer is correctly defined inside the class and I am able to use it in other areas of the code that aren't unit tests. Why am I getting this error? The second issue arises when I try to call a public method defined inside the CalculateConversationSinceTime class. Swift keeps telling me that the instance I created of CalculateConversationSinceTime is of type <<error type>>. Why is my instance not initializing properly? Could someone please provide some insight into why I am seeing these problems? Thanks.


Aucun commentaire:

Enregistrer un commentaire