我想單元測試的一些代碼,返回值看起來與此類似:如何模擬關閉以測試Grails服務結果?
Groovy Service code to test:
def findByThisAndThat(something) {
:
def items = []
sql.eachRow(query, criteriaList, {
def item = new Item(name:it.NAME)
items.add(item)
})
[items: items, whatever:whatevervalue]
}
單元測試代碼計劃:
void testFindByThisAndThatReturnsAMapContainingItems(){
Sql.metaClass.eachRow = { String query, List criteria, Closure c ->
// call closure to get passed in items list
// add one new Item(name:"test item") to list
}
def result = service.findByThisAndThat("", "")
assert result.items
assertEquals('test item', result.items[0].name)
}
我怎麼能這樣做? 謝謝!