Testing a method that has a `try-catch` inside it using XCTest

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)
.

The test stops at

data = try context.executeFetchRequest(request)
.

Does it stop? Or fail? If it fails, when failure does it report. If it stops, are you sure you don’t have a Swift Error Breakpoint set?

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I don't set any breakpoints. It just fails. How to report this kind of issue?

It just fails.

OK. When it fails, when does the failure look like? What do you see in Xcode’s debug output (View > Debug Area > Activate Console).

How to report this kind of issue?

If you want to report a bug about this, you can do so using the standard Bug Reporter site. It’d be best if you include a cut down version of your project that replicates the issue.

Please post your bug number, just for the record.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Testing a method that has a `try-catch` inside it using XCTest
 
 
Q