I am new with grails. In my web application I am using spock for unit test.

I have used searchable plugin for search functionality.

In my controller search method is defined as :

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])
}

while testing I have defined

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'

    }

But I cannot mock Question.search() method.

Error message receiving as

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

please anyone help me to resolve this issue.

Thanks in advance.

有帮助吗?

解决方案

I guess you are using the searchable plugin. If you want to test the search functionality write an IntegrationSpec. To Mock the search call for a Domainclass within a controller it would be best to wrap it in a service method and Mock the Service out.

And I think you do not want to test the constraints for the Question domain here.

Try something like that:

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'

}

You need to create the Service with a method that wraps the Question.search() command and use this service method in your controllers search method.

Something like that:

class QuestionService {

   def search(String query, def params) {

       Question.search(query, params)
   }

}

Your controllers search method:

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])
}

I hope you understand what I mean.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top