This is my method definition:
func isValidForMode(mode: DBFindViewControllerMode) throws -> Bool { }
Now I can test this in simple way, since I know that it DO NOT throws an error:
XCTAssertTrue(try! searchOptionsManager.isValidForMode(.Address))
But what if I know that method throws?
The best solution would be XCTAssertThrows()
, but it is not:-)
Below is my try:
do {
try searchOptionsManager.isValidForMode(.Address)
} catch let error {
XCTAssertEqual(error as! DBErrorType, DBErrorType.CannotBeEmpty("Street"))
}
But it fails, because:
Cannot find an overload for
XCTAssertEqual
that accepts an argument list of type(DBErrorType, DBErrorType)
Here is @robertvojta answer with several modification for Xcode 9 and Swift 3 - 4:
Usage:
Here is the example that you will understand try-catch check this below code
Hope you Understand
Make your
DBError
conforming toEquatable
:And then you can use it in
XCTAssertEqual
:Or create your own
XCTAssertThrows
.And:
Or simply use an optional
try
:No need to conform to
Equatable
The best solution so far I have found is:
This way you can test if exception is really thrown, but you cannot check what type of exception is thrown.
If you know that the function throws an error then you should also ensure that you fail in the event that an error is not thrown.
I'm modifying answers from @robertvojta and @vadim-bulavin here:
Usage: