gpt4 book ai didi

unit-testing - Grails 单元测试在其 Controller 中模拟域类

转载 作者:行者123 更新时间:2023-12-01 01:00:48 25 4
gpt4 key购买 nike

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

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

在我的 Controller 搜索方法定义为:

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

请任何人帮我解决这个问题。

提前致谢。

最佳答案

我猜您正在使用可搜索插件。
如果您想测试搜索功能,请编写一个 IntegrationSpec。
要在 Controller 中模拟域类的搜索调用,最好将其包装在服务方法中并模拟出服务。

而且我认为您不想在这里测试 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() 命令的方法创建服务,并在 Controller 搜索方法中使用此服务方法。

类似的东西:
class QuestionService {

def search(String query, def params) {

Question.search(query, params)
}

}

您的 Controller 搜索方法:
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])
}

我希望你明白我的意思。

关于unit-testing - Grails 单元测试在其 Controller 中模拟域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23601141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com