Grails单元测试其控制器内的模拟域类

时间:2014-05-12 04:38:10

标签: unit-testing grails mocking

我是grails的新手。在我的Web应用程序中,我使用spock进行单元测试。

我使用了可搜索的插件来搜索功能。

在我的控制器中,搜索方法定义为:

def search(SearchQueryCommand searchQueryCommand) {
        def question
        if (!searchQueryCommand.q?.trim() && !searchQueryCommand.tag?.trim() && !searchQueryCommand.name?.trim()) {
            params.sort='sortableTitle'
            params.order='desc'
            question = Question.search(MessageStatusEnum.ACTIVE.getType().toString(), params)
        }
    render(view: "include_questionlist", model: [questionList: question.results, totalSize:question.total , max:params.max?:'Constants.PAGINATION_DEFAULT_MAX', pagination:true ,questionDescription:true])
}

在测试时我已经定义了

void "action search questions : search with blank search query" () {

        setup:
        controller.params.q=""
        mockForConstraintsTests(Question)
        mockDomain(User)
        def question1 =new Question(title:'demo1', owner:new User(username:"Jini").save())
        def question2 =new Question(title:'demo2', owner:new User(username:"Jini").save())

        Question.search() >> [question1,question2]
        when:
        controller.search()

        then:
        view == '/question/include_questionlist'

    }

但我无法模拟Question.search()方法。

收到

的错误消息
Failure:  action search questions : search with blank search query(com.orb.question.QuestionControllerSpec)
|  groovy.lang.MissingMethodException: No signature of method: com.orb.question.Question.search() is applicable for argument types: (java.lang.String, org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap) values: [1992, [q:, sort:sortableTitle, order:desc]]
Possible solutions: each(groovy.lang.Closure), attach(), merge(), refresh(), save(), attach()
    at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:97)
    at com.orb.question.QuestionController.search(QuestionController.groovy:115)
    at com.orb.question.QuestionControllerSpec.action search questions : search with blank search query(QuestionControllerSpec.groovy:200)
| Completed 22 spock tests, 1 failed in 7480ms

请有人帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:4)

我猜你正在使用可搜索的插件。 如果要测试搜索功能,请编写IntegrationSpec。 要在控制器中模拟对Domainclass的搜索调用,最好将其包装在服务方法中并将服务模拟出来。

我认为你不想在这里测试Question域的约束。

尝试类似的东西:

void "action search questions : search with blank search query" () {

    setup:
    params.q=""
    mockDomain(Question)
    mockDomain(User)
    def question1 =new Question(title:'demo1', owner:new User(username:"Jini").save())
    def question2 =new Question(title:'demo2', owner:new User(username:"Jini").save())

    QuestionService fakeQuestionService = Mock()
    fakeQuestionService.search(_,_) >> [question1,question2]
    controller.questionService = fakeQuestionService

    when:
    controller.search()

    then:
    view == '/question/include_questionlist'

}

您需要使用包装Question.search()命令的方法创建服务,并在控制器搜索方法中使用此服务方法。

类似的东西:

class QuestionService {

   def search(String query, def params) {

       Question.search(query, params)
   }

}

您的控制器搜索方法:

def search(SearchQueryCommand searchQueryCommand) {
    def question

    if (!searchQueryCommand.q?.trim() && !searchQueryCommand.tag?.trim() && !searchQueryCommand.name?.trim()) {
        params.sort='sortableTitle'
        params.order='desc'
        question = questionService.search(MessageStatusEnum.ACTIVE.getType().toString(), params)
    }
    render(view: "include_questionlist", model: [questionList: question.results, totalSize:question.total , max:params.max?:'Constants.PAGINATION_DEFAULT_MAX', pagination:true ,questionDescription:true])
}

我希望你理解我的意思。