Since Groovy 1.8 we can check if a Map is equal to another Map if the keys and values are the same. Very convenient in tests for example.
def someMap = [age: 34, name: "Ted"] assert someMap == [name: "Ted", age: 34]
Today I kept staring at a failure, while testing some x and y graph data points returned by a Grails controller, where two Maps were somehow not equal according to Spock, while even the assertion’s output looked ‘equal’.
when: controller.milkYield() then: response.json and: "series are present" def series = response.json series.size() == 2 and: "realized series is correct" ... and: "predicted series is correct" def predictedSeries = series[1] predictedSeries.values.size() == 2 predictedSeries.values[0] == [y:null, x:'Mar'] predictedSeries.values[1] == [y:121, x:'Apr']
resulted in:
Condition not satisfied: predictedSeries.values[0] == [y:null, x:'Mar'] | | | | | | | false | | [y:null, x:Mar] | [[y:null, x:Mar], [y:121, x:Apr]] [values:[[y:null, x:Mar], [y:121, x:Apr]]]
Why isn’t [y:null, x:Mar]
equal to [y:null, x:Mar]
?
Read more over here.