Skip to content

Instantly share code, notes, and snippets.

@davetroy
Last active May 15, 2016 21:32
Show Gist options
  • Save davetroy/9242eeefde36755b411f37a462ec4598 to your computer and use it in GitHub Desktop.
Save davetroy/9242eeefde36755b411f37a462ec4598 to your computer and use it in GitHub Desktop.
import XCTest
@testable import NewApp
class FRCTests: CoreDataTestCase, NSFetchedResultsControllerDelegate {
var expectedProperties: [String:AnyObject]? = nil
var changeExpectation: XCTestExpectation?
var deleteExpectation: XCTestExpectation?
var insertExpectation: XCTestExpectation?
var fetchedResultsController: NSFetchedResultsController!
func updateAndWait(object: NSManagedObject, changes: [String:AnyObject]) {
object.setValuesForKeysWithDictionary(changes)
self.expectedProperties = changes
self.changeExpectation = expectationWithDescription("properties are correct")
NSLog("-- waiting for update to '\(changes)' --")
waitForExpectationsWithTimeout(1, handler: nil)
}
override func setUp() {
super.setUp()
let request = Address.fetch(predicate: nil, sortAttribute: "messageCount", ascending: true)
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: mainContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
try! fetchedResultsController.performFetch()
}
var itemCount: Int { return self.fetchedResultsController.sections![0].numberOfObjects }
func testFetchedResultsController() {
// check counts
XCTAssertEqual(itemCount, 0)
// insert
let address = Address.createEntity(inContext: mainContext) as! Address
address.mailbox = "[email protected]"
address.messageCount = 0
insertExpectation = expectationWithDescription("item is inserted")
try! mainContext.save()
waitForExpectationsWithTimeout(1, handler: nil)
XCTAssertEqual(itemCount, 1)
// updates
updateAndWait(address, changes: ["messageCount" : 10])
updateAndWait(address, changes: ["messageCount" : 12])
updateAndWait(address, changes: ["isContact" : true])
updateAndWait(address, changes: ["messageCount" : 119])
updateAndWait(address, changes: ["messageCount" : 103])
updateAndWait(address, changes: ["mailbox" : "[email protected]", "isContact" : false])
// delete
deleteExpectation = expectationWithDescription("item is deleted")
mainContext.deleteObject(address)
waitForExpectationsWithTimeout(1, handler: nil)
// check counts
XCTAssertEqual(itemCount, 0)
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
NSLog("inserted \(anObject.objectID) at \(newIndexPath!)")
insertExpectation?.fulfill()
insertExpectation = nil
case .Update, .Move:
let expectedKeys = Array(expectedProperties!.keys)
let actualValues = (anObject as! NSManagedObject).dictionaryWithValuesForKeys(expectedKeys) as NSDictionary
if actualValues.isEqualToDictionary(expectedProperties!) {
NSLog("got change: \(actualValues)")
changeExpectation?.fulfill()
}
case .Delete:
NSLog("deleted \(anObject.objectID) at \(indexPath!)")
deleteExpectation?.fulfill()
deleteExpectation = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment