The given, on, and it blocks

This style assumes the use of the following DSL blocks:

  • given: This block defines the context of our test. It can be a class that we want to test.
  • on: This block defines the function or action that we want to test.
  • it: This block defines an actual test. It can be the input or output of a function.

Let's test the set method of the ToDoStorage class. This test may look as follows:

@RunWith(JUnitPlatform::class)
object ToDoSpek : Spek({
given("A storage") {
val storage = ToDoStorage()
on("set a todo with with args: name and context") {
val todo = ToDo("name", "content")
val result = storage.set("name", todo)
it("returns true") {
assert(result)
}
}
}
})

Press the Run button and you will see the following window:

As you can see, the output not only shows that a test has passed successfully, but also contains a description of the test case. We can add one more block to test the get method. This test may look as follows:

on("""get a todo by "name" key""") {
val todo = storage["name"]
it("""returns a todo with "content" """) {
assertEquals("content", todo?.content)
}
}

The run window looks as follows:

You can add as many on blocks as you want. You can also create different context blocks in the same Spek class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.64