Search code examples
scalabddagilespecs2

Specs2 - How to define complex objects for Given/When/Then steps


Specs2's documentation and samples show some codes about the use of Given/Then/When style in an acceptance test.

Here one of them:

"A given-when-then example for the addition"                   ^
    "Given the following number: ${1}"                         ^ number1 ^
    "And a second number: ${2}"                                ^ number2 ^
    "And a third number: ${3}"                                 ^ number3

  val number1: Given[Int]               = (_:String).toInt
  val number2: When[Int, (Int, Int)]    = (n1: Int) => (s: String) => (n1, s.toInt)
  val number3: When[Seq[Int], Seq[Int]] = (numbers: Seq[Int]) => (s: String) => numbers :+ s.toInt

However, it only deal with some primitive objects as Int here (normal since it's a sample).

But how to deal with complex objects? Using Datatables?

Ideal would be to define, within some file of "fixtures", datatables (or similar things) defining complex objects.

External datatable defining the User "Mik"

"name" |   "job"    | "e-mail"
"mik"  | "engineer" | "[email protected]"

I would like to create a Given statement like this one:

"Given the following user: ${Mik}"       ^ retrieve_user_from_database ^

val user: Given[User]               = .....Some ideal codes here to map Mik's values to User model.

What is a good practice and what does Specs2 currently provide?


Solution

  • If you want to retrieve a user from an existing database, you can do the following:

    "Given the following user: ${Mik}" ^ user ^
    
    val user: Given[User] = (name: String) => database.getUserByName(name) 
    

    Now you might want to populate your database with users before doing that. One possibility is indeed to use Datatables:

    def populateDatabase = 
      "name" ||   "job"    | "e-mail"        |
      "mik"  !! "engineer" ! "[email protected]"   |
      "eric" !! "engineer" ! "[email protected]" | { (name, job, email) => 
        database.saveUser(User(name, job, email)) must not(throwAn[Exception])
      }
    

    and put this in a Step before your G/W/T group:

    Step(populateDatabase) ^
    "Given the following user: ${Mik}"  ^ user ^
    "This user should be an ${engineer} ^ jobOk ^
                                          end
    
    val user: Given[User] = (name: String) => database.getUserByName(name)
    val jobOk: Then[User] = (user: User) => (job: String) => user.job must_== job