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.