I have a method that receives a ContactForm
object and a map with recipients. So I wrote the following spec that the correct recipient from the Map is returned, based on the inquiry of the form:
def "Correct recipients for first inquiry"() {
setup:
def form = Mock(ContactForm)
form.getInquiry() >> "Subject 1"
expect:
sut.getRecipients(form, recipientsTestMap) == ["recipient1"]
}
def "Correct recipients for second inquiry"() {
setup:
def form = Mock(ContactForm)
form.getInquiry() >> "Subject 2"
expect:
sut.getRecipients(form, recipientsTestMap) == ["recipient2"]
}
// and so on ...
Is there a data-driven way to do this? Unfortunately, not passing the form but the inquiry string itself is not an option right now, since this would require massive refactoring. I was just curious if it is possible with Spock to do this data-driven although the mock has to be changed before each test.
You can do this in the following way (not sure if this is what You're asking for):
@Unroll
def "Correct recipients for #inquiry inquiry"() {
setup:
def form = Mock(ContactForm)
form.getInquiry() >> inquiry
expect:
sut.getRecipients(form, recipientsTestMap) == result
where:
inquiry | result
"Subject 1" | ["recipient1"]
"Subject 2" | ["recipient2"]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With