Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

Controller tests

Now let's fix the controller tests. The first one will be the product controller. Your ProductControllerTestSpec.scala file should look like this:

class ProductControllerTestSpec

extends

PlaySpec

with OneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory { "ProductController" should {

DBCleaner.cleanUp()

"insert a new product should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product")

click on id("price")

enter("17.55")

submit()

}

"details from the product 1 should be ok" in {

goTo(s"http://localhost:${port}/product/details/1") textField("name").value mustBe "Blue Ball"

textField("details").value mustBe "Blue Ball is a Awesome and simple product"

textField("price").value mustBe "17.55"

}

"update product 1 should be ok" in {

goTo(s"http://localhost:${port}/product/details/1") textField("name").value = "Blue Ball 2"

textField("details").value = "Blue Ball is a Awesome and simple product 2 "

textField("price").value = "17.66" submit()

goTo(s"http://localhost:${port}/product/details/1") textField("name").value mustBe "Blue Ball 2" textField("details").value mustBe "Blue Ball is a Awesome and simple product 2 "

textField("price").value mustBe "17.66"

}

"delete a product should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product")

click on id("price")

enter("17.55")

submit()

goTo(s"http://localhost:${port}/product") click on id("btnDelete")

}

"Cleanup db in the end" in {

DBCleaner.cleanUp()

}

}

}

The Controller product test needs to call the DBCleaner function at the beginning of the test to make sure that the database is in a well-known state; additionally, and after running all the tests, we need to clean up the database just to be safe. We will now apply the same changes for the review and image controller tests. Your ReviewControllerTestSpec file should look like this:

class ReviewControllerTestSpec

extends PlaySpec

with OneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory { DBCleaner.cleanUp()

"ReviewController" should {

"insert a new review should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product")

click on id("price")

enter("17.55")

submit()

goTo(s"http://localhost:${port}/review/add")

singleSel("productId").value = "1"

click on id("author")

enter("diegopacheco")

click on id("comment")

enter("Tests are amazing!")

submit()

}

"details from the review 1 should be ok" in {

goTo(s"http://localhost:${port}/review/details/1") textField("author").value mustBe "diegopacheco" textField("comment").value mustBe "Tests are amazing!"

}

"update review 1 should be ok" in {

goTo(s"http://localhost:${port}/review/details/1") textField("author").value = "diegopacheco2" textField("comment").value = "Tests are amazing 2!" submit()

goTo(s"http://localhost:${port}/review/details/1") textField("author").value mustBe "diegopacheco2" textField("comment").value mustBe "Tests are amazing 2!"

}

"delete a review should be ok" in {

goTo(s"http://localhost:${port}/review/add")

singleSel("productId").value = "1"

click on id("author")

enter("diegopacheco")

click on id("comment")

enter("Tests are amazing!")

submit()

goTo(s"http://localhost:${port}/review") click on id("btnDelete")}

"Cleanup db in the end" in {

DBCleaner.cleanUp()

}

}

}

Alright, we have the tests for the review controller fixed. Now we can move to the last controller test for the image. Your ImageControllerTestSpec.scala file should look like this:

class ImageControllerTestSpec

extends PlaySpec

with OneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory { DBCleaner.cleanUp()

"ImageController" should {

"insert a new image should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product") click on id("price") enter("17.55")

submit()

goTo(s"http://localhost:${port}/image/add") singleSel("productId").value = "1" click on id("url")

enter("https://thegoalisthering.files.wordpress.com/2012/01/

bluetennisball_display_image.jpg")

submit()

}

"details from the image 1 should be ok" in {

goTo(s"http://localhost:${port}/image/details/1") textField("url").value mustBe

"https://thegoalisthering.files.wordpress.com/2012/01/

bluetennisball_display_image.jpg"

}

"update image 1 should be ok" in {

goTo(s"http://localhost:${port}/image/details/1") textField("url").value =

"https://thegoalisthering.files.wordpress.com/2012/01/

bluetennisball_display_image2.jpg"

submit()

goTo(s"http://localhost:${port}/image/details/1") textField("url").value mustBe

"https://thegoalisthering.files.wordpress.com/2012/01/

bluetennisball_display_image2.jpg"

}

"delete a image should be ok" in {

goTo(s"http://localhost:${port}/image/add") singleSel("productId").value = "1" click on id("url")

enter("https://thegoalisthering.files.wordpress.com/2012/01/

bluetennisball_display_image.jpg")

submit()

goTo(s"http://localhost:${port}/image") click on id("btnDelete")

}

"Cleanup db in the end" in {

DBCleaner.cleanUp()

}

}

}

All right, all the controller tests are fixed now. We can run all the tests to double check whether everything is OK. Run the following command:

$ activator test

Persistence with Slick

You get output as shown in the following screenshot:

If you have problems running the application (covered in the next section), apply the evolution, and then you can run the tests again. Tests might take some time, depending on your hardware.

Running the application

Now it is time to run the application using $ activator run. Open your web browser, and go to http://localhost:9000/. Once you do that, Play will detect that the application needs evolutions, and will apply the three evolutions we have (l.sql, 2.sql, and 3.sql). However, you will need to click on the button to apply the evolution.

After you click on the red button, Apply this script now!, Slick will create the tables, and redirect you to the application.

Summary

With this, we reach the end of the chapter. You learned how to perform database persistence using Slick. You also learned how to do FRM mapping, and we refactored our application and tests so they work with reactive persistence and the Play framework. We then explained how to access the database using Scala code, and perform operations using SQL.

In the following chapter, we will see more about reports, and we will use our database to generate reports based on our Play framework application.

7

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel