Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

How to test Errors in Swift

by fragi
May 24, 2019
in Unit Testing
Share on FacebookShare on Twitter

Handling all the error scenarios of our functionality is very important. Swift provides a nice way to handle Errors. The happy path is, of course, the one that takes most of the focus and is usually the first one that is being tested. But that should not leave the alternative or error paths untested. 

Errors in Swift are values of types that conform to the Error protocol. Normally we group related errors in enumerations.

 

Writing tests to verify that a function is throwing an Error is easy in Swift. 

Let’s imagine that we have the FizzBuzz Kata but instead of return “Fizz”, “Buzz” or “FizzBuzz” we have to throw an Error. Our code will look like:

And we can use XCTAssertThrowsError to catch the Error and then use the XCTAssertEquals to check if it is the expected Error.

This logic of XCTAssertThrows and XCTAssertEqual can be useful in many tests, so instead of repeat the code we can use an extension with this logic (https://www.swiftbysundell.com/posts/testing-error-code-paths-in-swift)

And the tests can look like :

 

Github code:

https://github.com/Fragki/how-to-unit-test-serries

Resources:

https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html

https://www.swiftbysundell.com/posts/testing-error-code-paths-in-swift

 

fragi

fragi

Related Posts

What is TDD?
Unit Testing

Write better Unit tests with XCTUnwrap

January 27, 2022

Testing optionals can require boilerplate code to unwrap it. It can also affect the readability of the test. Let's look...

Test static method on collaborator
Unit Testing

Test static method on collaborator

May 13, 2021

One of the main issues we are facing when we try to make an old piece of code testable is...

Dependency injection – UIViewController in Storyboard (iOS 13)
Unit Testing

Dependency injection – UIViewController in Storyboard (iOS 13)

May 13, 2021

Finally Apple (iOS 13 +) has added constructor dependency injection to Storyboards! No excuses anymore for not using DI properly...

Unit testing code with DispatchQueue
Unit Testing

Unit testing code with DispatchQueue

May 13, 2021

One way of testing code with DispatchQueue is by using expectations (https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations) but in most of the cases it's more...

Unit testing UIViewController LifeCycle
Unit Testing

Unit testing UIViewController LifeCycle

May 13, 2021

Unit testing UIViewController life cycle events are not a straightforward process. Let's have a look on the following view controller:...

Dependency Injection – UIViewController with a Nib file
Unit Testing

Dependency Injection – UIViewController with a Nib file

May 13, 2021

UIViewControllers are among the most used UIKit objects. As the name implies UIViewControllers are controller objects and their purpose should...

Next Post
FizzBuzz Throw Kata

FizzBuzz Throw Kata

Roman Numerals

Roman Numerals

Improve pull request descriptions using templates

Improve pull request descriptions using templates

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.