jeudi 5 novembre 2015

Verifying more than one outputs in the same test case, using a Spock where: block

I am trying to verify two different outputs in the context of a single Spock method that runs multiple test cases of the form when-then-where.

For this reason I use two assertions at the then block, as can be seen in the following example:

import spock.lang.*

@Unroll
class ExampleSpec extends Specification {

     def "Authentication test with empty credentials"() {
         when:
         def reportedErrorMessage, reportedErrorCode
         (reportedErrorMessage, reportedErrorCode) = userAuthentication(name, password)

         then:
         reportedErrorMessage == expectedErrorMessage
         reportedErrorCode    == expectedErrorCode

         where:
         name | password || expectedErrorMessage | expectedErrorCode
         ' '  | null     || 'Empty credentials!' | 10003
         ' '  | ' '      || 'Empty credentials!' | 10003
     }
}

The problem that I have is this: if neither reportedErrorMessage and reportedErrorCode correspond to their respective expected values in the same test case, then I get a failing message only for the first assertion (here for reportedErrorMessage). I have seen similar-looking code elsewhere, so I assume that it has worked for them. I does not work for me though. (I have also tried it with an expect-where structure, to no avail.) Am I doing something wrong?

EDIT:

I am adding a piece of code that demonstrates the same problem without other external code dependencies. I understand that in this particular case it is not a good practice to bundle two very different tests together, but I think it still demonstrates the problem.

import spock.lang.*

@Unroll
class ExampleSpec extends Specification {

    def "minimum of #a and #b is #c and maximum of #a and #b is #d"() {
        expect:
        Math.min(a, b) == c
        Math.max(a, b) == d

        where:
        a | b || c | d
        3 | 7 || 3 | 7
        5 | 4 || 5 | 4  // <--- both c and d fail here
        9 | 9 || 9 | 9
    }
}

Aucun commentaire:

Enregistrer un commentaire