Home Computer Science Building Applications with Scala
Unit testing principles
Unit testing is the smallest unit of testing that you could possibly apply. You need to apply it at the class level. So a unit test will cover your class with all the functions you have there. But hold on a minute, a class often has dependencies, and these dependencies might have other dependencies, so how do you test that? We need to have mocks, simple dumb objects that simulate other classes' behavior. This is an important technique to isolate code and allow unit testing.
Making code testable
Unit testing is simple: basically, we call a function by passing arguments to it, and then we check the output to see if it matches our expectations. This is also called asserts or assertions. So, unit testing is about asserts. Sometimes, your code might not be testable. For instance, let's say you have a function that returns a unit and has no parameters. This is very tough to test, because it implies that the function is full of side-effects. If you remember what we discussed in chapter 1, Introduction to FP, Reactive, and Scala, this is against FP principles. So, if we have this case, we need to refactor the code to make the function return something, and then we can test it.
|< Prev||CONTENTS||Next >|