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

笑着哭i 提交于 2020-01-06 07:56:06

问题


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" | "mik@mik.com"

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?


回答1:


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" ! "mik@mik.com"   |
  "eric" !! "engineer" ! "eric@eric.com" | { (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


来源:https://stackoverflow.com/questions/14507098/specs2-how-to-define-complex-objects-for-given-when-then-steps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!