Test process development: how to test your forms in the gaming infrastructure using scala?

I am working on a form for my web application, I would like to check the input fields in the form I created to make sure that they will produce the desired results. I am completely new to TDD, so I'm not sure how to make fake inputs to test my restrictions that I have in the input fields.

Can anyone suggest any documentation on this or post some sample code?

Thank you very much.

+4
source share
1 answer

Here is a basic test:

import org.scalatest.FunSpec
import play.api.data.Form
import play.api.data.Forms._

class TddFormTest extends FunSpec {
  case class Data(name: String, age: Int)
  val form = Form(mapping("name" -> text(minLength = 1, maxLength = 25), "age" -> number)(Data.apply)(Data.unapply))
  describe("a basic form") {
    it("accepts name input") {
      form.bind(Map("name" -> "Exactly 25 characters...."))
    }

    it("requires name to be non-empty") {
      val boundForm = form.bind(Map("name" -> ""))
      assert(boundForm.hasErrors)
      assert(boundForm.errors("name").head.message == "error.minLength")
    }

    it("does not accept names over 25 characters") {
      val boundForm = form.bind(Map("name" -> "whoah this name is too long!!!!!!"))
      assert(boundForm.hasErrors)
      assert(boundForm.errors("name").head.message == "error.maxLength")
    }

    it("fills from Data") {
      val filledForm = form.fill(Data("Testerton Testertop", 42))
      assert(!filledForm.hasErrors)
    }

    it("binds to Data") {
      val name = "Testerton Testertop"
      val age = 42
      val boundForm = form.bind(Map("name" -> name, "age" -> age.toString))
      assert(Data(name, age) == boundForm.get)
    }
  }
}

, , , . Request, . , , , . - : https://www.playframework.com/documentation/2.3.x/ScalaFunctionalTestingWithScalaTest

0

Source: https://habr.com/ru/post/1529147/


All Articles