How to define context for data tables in specs2

I am trying to define some context so that it is executed for each row of the data table (before the statement is executed on each row).

I found this example, but for life I can’t understand how to write a complete set of tests. I would like to define the context once and share it with all the examples. Here is roughly what I have:

class SomeSuite extends Specification with DataTables { // TODO: define context somehow??? // val context = new Before { println("BEFORE") } "test 1" should { "do something" in { context | "col1" | "col2" | val1 ! val2 | val3 ! val4 |> { (a, b) => //some assertion with (a, b) } } } } 

I would like to see that "DO" is printed every time (only 2 times) before each statement with (a, b).

I would really appreciate any help.

Thanks;)

Thanks Eric, here is my last code. I only added “implicit” as the context is used for many tests:

 class SomeSuite extends Specification with DataTables { implicit val context = new Before { def before = println("BEFORE") } "test 1" should { "do something" in { "col1" | "col2" | val1 ! val2 | val3 ! val4 |> { (a, b) => a must_== b // this is wrapped with context } } } } 
+4
source share
1 answer

A simple way is to use the apply method for Context

 class SomeSuite extends Specification with DataTables { val context = new Before { def before = println("BEFORE") } "test 1" should { "do something" in { "col1" | "col2" | val1 ! val2 | val3 ! val4 |> { (a, b) => context { a must_== b } } } } } 
+5
source

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


All Articles