模拟使用findAll,生成MissingMethodException的grails方法

时间:2011-02-17 20:27:00

标签: unit-testing grails junit gorm

def retrieveEatenFood(String token, String addedDate) {     

    def consumer = Consumer.findByMobileToken(token)
    if(consumer != null) {  

        def efList = []

        def list = consumer.findAll("from EatenFood as ef where date(ef.dateAdded) =  date(:da)",[da:sdf_long.parse(addedDate)])
        list.each{
            def eatenList = [:]
            eatenList.put("foodType",it.food.name)
            eatenList.put("sequenceNumber",it.sequenceNumber)
            eatenList.put("eatenDate", it.eatenDate)
            eatenList.put("DateAdded",it.dateAdded)
            efList.add(eatenList);
        }

        return efList;  
    }
}

尝试模拟上述方法,但findAll会不断生成异常。

这个问题有效!现在我需要为它编写测试,并且我一直得到这个例外。任何人都可以帮助我!

groovy.lang.MissingMethodException: No signature of method: carrotdev.Consumer.findAll() is applicable for argument types: (java.lang.String, java.util.LinkedHashMap) values: [from EatenFood as ef where date(ef.dateAdded) =  date(:da), [da:Sun Feb 13 01:51:47 AST 2011]]
Possible solutions: findAll(groovy.lang.Closure), find(groovy.lang.Closure)
    at carrotdev.ConsumerService.retrieveEatenFood(ConsumerService.groovy:146)
    at carrotdev.ConsumerService$retrieveEatenFood.call(Unknown Source)
    at carrotdev.ConsumerServiceTests.testEatenFoodRetrievedSucessfully(ConsumerServiceTests.groovy:359)

1 个答案:

答案 0 :(得分:7)

我会将查询移到带有描述性名称的Consumer域类,例如

static List<EatenFood> findAllEatenByDate(String date) {
   consumer.findAll(
      "from EatenFood as ef where date(ef.dateAdded) = date(:da)",
      [da:sdf_long.parse(addedDate)])
}

然后电话就是

def list = Consumer.findAllEatenByDate(addedDate)

你可以用

轻松嘲笑
def foods = [new EatenFood(...), new EatenFood(...), ...]
Consumer.metaClass.static.findAllEatenByDate = { String date - > foods }

请务必在消费者集成测试中测试取景器方法。