What is a good style for preconditions at Spock?

In the attribute method, one indicates the action of the function in the when: block, the result of which is checked in the subsequent then: block. Often, preparation is required, which is done in the given: (or setup: or fastening method) clause. It is equally useful to include preconditions: these are conditions that are not the subject of a feature test (thus, they should not be in when: - then: or expect: , but they state / document the necessary conditions for the test to be significant. See for example the dummy parameter below:

 import spock.lang.* class DummySpec extends Specification { def "The leading three-caracter substring can be extracted from a string"() { given: "A string which is at least three characters long" def testString = "Hello, World" assert testString.size() > 2 when: "Applying the appropriate [0..2] operation" def result = testString[0..2] then: "The result is three characters long" result.size() == 3 } } 

What is the suggested practice for these prerequisites? I used assert in this example, but many are unhappy with assert in Spec.

+6
source share
1 answer

I have always used expect for such scenarios:

 @Grab('org.spockframework:spock-core:0.7-groovy-2.0') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class DummySpec extends Specification { def "The leading three-caracter substring can be extracted from a string"() { given: "A string which is at least three characters long" def testString = "Hello, World" expect: "Input should have at least 3 characters" testString.size() > 3 when: "Applying the appropriate [0..2] operation" def result = testString[0..2] then: "The result is three characters long" result.size() == 3 } } 
+6
source

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


All Articles