gpt4 book ai didi

grails - 测试 Controller 时Spock和Grails 2.0.1的问题

转载 作者:行者123 更新时间:2023-12-02 14:26:41 25 4
gpt4 key购买 nike

在运行Spock Controller 测试时:

导入grails.plugin.spock。*
导入grails.test.mixin。*

@Mock(MusicItem)
类MusicItemControllerSpec扩展ControllerSpec {

def "Creating element with correct parameters"() {
setup:
controller.params.artist = "John Lennon"
controller.params.name = "Imagine"
controller.params.location = new URL ("http://localhost")

when:
controller.save()

then:
redirectArgs.action == "show"
controller.flash.message != null
MusicItem.count() == 1
}

}

被测试的零件在哪里:
def save() {
def musicItemInstance = new MusicItem(params)
if (!musicItemInstance.save(flush: true)) {
render(view: "create", model: [musicItemInstance: musicItemInstance])
return
}

flash.message = message(code: 'default.created.message', args: [message(code: 'musicItem.label', default: 'MusicItem'), musicItemInstance.id])
redirect(action: "show", id: musicItemInstance.id)
}

那是 Controller 的一部分。我收到此异常:

| Running 6 spock tests... 3 of 6 | Failure: Creating element with correct parameters(myvibe.fondation.MusicItemControllerSpec) | java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at myvibe.fondation.MusicItemController.save(MusicItemController.groovy:29) at myvibe.fondation.MusicItemControllerSpec.Creating element with correct parameters(MusicItemControllerSpec.groovy:39) | Running 6 spock tests... 4 of 6



当我从save()方法删除此行时:
        flash.message = message(code: 'default.created.message', args: [message(code: 'musicItem.label', default: 'MusicItem'), musicItemInstance.id])

比一切正常。我缺少什么?

最佳答案

由于消息标签而失败。以下应该工作

setup:
controller.metaClass.message = {args -> "mockMessage"}

http://greybeardedgeek.net/2011/05/13/testing-grails-controllers-with-spock/

关于grails - 测试 Controller 时Spock和Grails 2.0.1的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10035501/

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