Skip to content

Instantly share code, notes, and snippets.

@rpapallas
Created December 23, 2016 14:32
Show Gist options
  • Save rpapallas/8e239dc75586684fae99b0d75ed41169 to your computer and use it in GitHub Desktop.
Save rpapallas/8e239dc75586684fae99b0d75ed41169 to your computer and use it in GitHub Desktop.
Minimal Example of Unit Testing Core Data
import XCTest
import CoreData
@testable import DoIt
/// Test the DatabaseManager against a Mock Database Manager.
///
/// Note that testing is done against a mock version of the database, linking
/// to the memory instead of the actual SQLite file.
///
/// - Author:
/// Rafael Papallas
class DatabaseManagerTests: XCTestCase {
override func setUp() {
super.setUp()
}
/// This will test if the database was able to insert a new task instance.
///
/// Note that this will not test if the instance could be located after
/// insertion, is just going to check if the `.saveOrUpdate(instance:)`
/// returned true, which will indicate that the insertion was successful.
func testSuccessfulTaskInsertion_WithAllAttributesSet() {
// Create new task instance.
let mockDatabaseManager = MockDatabaseManager()
let newTask = mockDatabaseManager.getEmptyTaskInstance
newTask.title = "Test #1"
XCTAssertTrue(mockDatabaseManager.saveOrUpdate(instance: newTask))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment