1
func orderPizza() throws

enum PizzaOrderError: Error {
    case pizzaServiceNotAnsweringPhone
    case unexpectedAnswer(String)
}

// Test that orderPizza throws any PizzaOrderError
expect { try orderPizza() }.to(throwError(errorType: PizzaOrderError.self))

// Test that orderPizza throws a .pizzaServiceNotAnsweringPhone error
expect { try orderPizza() } 
    .to(throwError(PizzaOrderError.pizzaServiceNotAnsweringPhone))

Given the abovementioned interface, how can I unit test, that orderPizza() throws any PizzaOrderError.unexpectedAnswer without checking the String?

Something like matching throwError(PizzaOrderError.unexpectedAnswer) doesn't seem to be possible, as the compiler always expects a specific String argument to be provided with the reference error.

Nimble has a way to provide a closure to perform "arbitrary custom matching". I haven't been able to find any examples of how this should work and am a little bit confused about the return type of this closure being Void. I would have expected something like Bool.

How would I indicate success or failure in such a matching closure? And is there any shorter or cleaner way to extract a boolean information about an enum case match than this?

if case PizzaOrderError.unexpectedAnswer(_) = error {
    return true
} else {
    return false
}
Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
  • expect({ guard case PizzaOrderError.unexpectedAnswer(_) = error else { return .failed(reason: "wrong enum case") } return .succeeded }).to(succeed()) – Sachin Vas Feb 12 '19 at 06:20

1 Answers1

0

Matching specific error types and cases works like this:

.to(throwError(errorType: PizzaOrderError.self) {
    expect($0) == .pizzaServiceNotAnsweringPhone
}
mbi
  • 581
  • 5
  • 13