The describe and it blocks

This style differs from the previous one by using the describe block. The describe block is also used to define a context but we can also nest one describe block into another to provide a more detailed context. Let's create the DescribeStyleToDoSpek object to demonstrate this. Initially, it may look as follows:

@RunWith(JUnitPlatform::class)
object DescribeStyleToDoSpek : Spek({
describe("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)
}
}
on("""get a todo by "name" key""") {
val todo = storage["name"]
it("""returns a todo with "content" """) {
assertEquals("content", todo?.content)
}
}
}
})

This object contains a specification with a single context that is defined by the describe block. After running these tests, you will see the following window:

Let's add a new describe block to check that a non-empty storage throws ToDoAlreadyException if a user tries to set a todo with a preexisting key. This may look as follows:

describe("a non-empty storage") {
on("""set a todo with a key that already stored""") {
it ("should throw ToDoAlreadyExistException") {
shouldThrow<ToDoAlreadyExistException> {
val todo = ToDo("name", "content")
storage.set("name", todo)
}
}
}
}

The run window looks as follows:

As you can see, a nested describe block can be useful if we want to define a more specific context of our test case.

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

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