Product test

We should also verify that three product rows are inserted when the application is started.

Create a test class, named ProductDaoSpec, which extends PlaySpec. Now, PlaySpec is the integration of ScalaTest in Play. The ProductDaoSpec class also needs to extend the GuiceOneAppPerSuite trait. This trait adds a new instance of the Application object to the ScalaTest suite:

class ProductDaoSpec extends PlaySpec with ScalaFutures with GuiceOneAppPerSuite {
"ProductDao" should {
"Have default rows on database creation" in {
val app2dao = Application.instanceCache[ProductDao]
val dao: ProductDao = app2dao(app)

val expected = Set(
Product("PEPPER", "ALD2", "PEPPER is a robot moving with wheels
and with a screen as human interaction"
, 7000),
Product("NAO", "ALD1", "NAO is an humanoid robot.", 3500),
Product("BEOBOT", "BEO1", "Beobot is a multipurpose robot.",
159.0)
)

dao.all().futureValue should contain theSameElementsAs (expected)
}
}
}

Play provides a helper method to create an instance in the cache. As you can see, app2dao can create an instance of ProductDao, which was the type parameter passed to instanceCache.

The matcher on the Set is not strict, and it does not take into account the order of the rows received. If you would like to be stricter, ScalaTest provides the theSameElementsInOrderAs matcher, which checks the order of the elements in the collection. 

As the dao.all() function returns Future, ScalaTest provides the .futureValue helper to wait on Future to finish before testing the value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset