Skip to content

Instantly share code, notes, and snippets.

@sgoodwin
Created February 28, 2020 10:55
Show Gist options
  • Save sgoodwin/e3249abd9861632dbd622f5365bf6448 to your computer and use it in GitHub Desktop.
Save sgoodwin/e3249abd9861632dbd622f5365bf6448 to your computer and use it in GitHub Desktop.
How to do "assert no difference" and "assert difference" in Swift
func XCTAssertNoDifference<T>(_ context: NSManagedObjectContext, request: NSFetchRequest<T>, file: StaticString = #file, line: UInt = #line, work: () -> Void) {
let oldCount = try! context.count(for: request)
work()
let newCount = try! context.count(for: request)
XCTAssertEqual(oldCount, newCount, "Count incorrectly changed: \(oldCount) to \(newCount)", file: file, line: line)
}
func XCTAssertDifference<T>(_ context: NSManagedObjectContext, request: NSFetchRequest<T>, difference: Int, file: StaticString = #file, line: UInt = #line, work: () -> Void) {
let oldCount = try! context.count(for: request)
work()
let newCount = try! context.count(for: request)
XCTAssertEqual(oldCount + difference, newCount, "Count incorrectly changed: \(oldCount) + \(difference) to \(newCount)", file: file, line: line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment