Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data-driven test with mocking

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.

like image 538
Sebastian Wramba Avatar asked Sep 17 '14 12:09

Sebastian Wramba


1 Answers

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"]
}
like image 155
Opal Avatar answered Sep 29 '22 10:09

Opal