Testability
Our mutable example's addNewProduct() method has no return values. While it is possible to write a unit test for it, it is not obvious how we should implement asserting because the method causes a side-effect in our existing instance that we need to know about.
Our immutable example's addNewProduct() method returns a new array of product. We simply inspect the value of products and assert. We still have both the old and new instances, so we have everything we need to ensure that our code works as intended.
Although we do not cover unit-testing in this book, it is highly recommended that you explore QuickCheck-based libraries such as Quick (https://github.com/Quick/Quick) and SwiftCheck (https://github.com/typelift/SwiftCheck
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access