jeudi 2 avril 2015

How to match collection contents in Spock interactions?

Given I have the following method in a class that I mock:



class Foo {
public void doSomething(Collection<String> input) {
//...
}
}


Now I mock this class in my Spock test and I want to verify an interaction:



def test() {
setup:
def myMock = Mock(Foo)

when:
def hashSet = new HashSet<String>(['foo', 'bar'])
myMock.doSomething(hashSet)

then:
1 * myMock.doSomething(['foo', 'bar'])

}


This interaction however doesn't trigger. What is really strange is that the output is telling me:



too few invocations for:

1 * myMock.doSomething(['foo', 'bar']) (0 invocations)

Unmatched invocations (ordered by similarity):

1 * myMock.doSomething(['foo', 'bar'])


So it basically told me that there was no invocation that looked like the one I was expecting but there was another one which ... ermm looked like the one I was expecting.


Am I doing something wrong here or is this a limitation of Spock and I need to check the collection contents in a closure like



1 * mock.doSomething( { it == ['foo', 'bar'] })

Aucun commentaire:

Enregistrer un commentaire