I'm trying to test a method with a
try-catch in it but the test does not continue and the catch block is not being considered.Let's say we have this:
class Foo
{
func fetchData(request: NSFetchRequest) -> [AnyObject]?
{
var data: [AnyObject]? = nil
do {
data = try context.executeFetchRequest(request)
} catch {}
return data
}
func fetchData(entity: String, predicate: NSPredicate?) -> [AnyObject]?
{
let entity = NSEntityDescription.entityForName(entity, inManagedObjectContext: context)
let request = NSFetchRequest()
request.entity = entity
request.predicate = predicate
return fetchData(request)
}
}
class SampleTests: XCTestCase
{
// Assume that entity 'Employee' does not exist in the core data model.
func testFetchDataWithNonExistingEntity()
{
let foo = Foo()
let predicate = NSPredicate(format: "id == %i", 22345)
let data = foo.fetchData("Employee", predicate: predicate)
XCTAssertNil(data, "Data should be nil.")
}
}The test stops at
data = try context.executeFetchRequest(request).