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