Unit Tests
Learn how to implement a base class and tests for unit testing.
Base class for unit tests
To avoid repeating the construction of our unit test classes, we will implement a base class for tests, which is quite simple.
Press + to interact
abstract class BaseSpec extends WordSpecwith MustMatchers with ScalaCheckPropertyChecks {}
We will be leaning towards the more verbose test styles, but we can essentially use any of the other test styles that ScalaTest offers.
Testing Product
fromDatabase
Press + to interact
import com.wegtam.books.pfhais.impure.models.TypeGenerators._forAll("input") { p: Product =>val rows = p.names.map(t => (p.id, t.lang.value, t.name.value)).toListProduct.fromDatabase(rows) must contain(p)}
The code above is a straightforward test of our helper function fromDatabase
, which works in the following way:
- The
forAll
will generate a lot ofProduct
entities using the generator (Line 3). - From each entity, a list of “rows” is constructed as they would appear in
Access this course and 1400+ top-rated courses and projects.