mardi 31 mai 2016

Checking Mock Parameter with Spock

I am unit testing some existing code and I have just started learning Spock.

I am trying to verify the SUT calls a Collaborator correctly, the method takes a Function as an argument which is generated by SUT.

What is the best way to check that the function is behaving as expected?

I've tried the below, which validates SUT calls its collaborator the correct number of times with a Function, which seems ok, but

def collab = Mock(Collab)
def sut = new SUT(collab)
def input = new Mock(Input)

def "..."() {

    when:
        sut.generatesAFunctionAndCallsCollab(input)
    then:
        1 * input.methodCalledInFunction() >> inputRetn
        1 * collab.methodCall({ Function fun ->
            assert fun.apply(input) == expectedResult
        })
    where:
        inputRetn | expectedResult
        true      | false
        false     | true
}

Finally, how could I refactor the closure into one place to reuse across tests?

Aucun commentaire:

Enregistrer un commentaire