如何在集成测试用例中模拟 Grails 配置?
考虑以下场景
MyController.groovy
def save() {
baseLink = Holders.getFlatConfig()["grails.test.base.link"]
if (!baseLink) {
response.status = HttpStatus.NOT_ACCEPTABLE.value
respond([message: "Configuration not found."])
return
}
// Some Code
}
MyControllerIntegrationSpec.groovy
def save() {
baseLink = Holders.getFlatConfig()["grails.test.base.link"]
if (!baseLink) {
response.status = HttpStatus.NOT_ACCEPTABLE.value
respond([message: "Configuration not found."])
return
}
// Some Code
}
def setup() {
//Some Setup Code
//Update configuration
grailsApplication.config["grails.test.base.link"] = true
}
void "Configuration not found"() {
when: ""
myController.save()
then: "Configuration not found"
controller.response.json["message"] == "Configuration not found."
controller.response.status == HttpStatus.NOT_ACCEPTABLE.value
}
void "Configuration found"() {
when: ""
myController.save()
then: "Configuration found"
//some code
}
最佳答案
假设您知道为什么要在集成测试中模拟 grails 应用程序,我的建议是使用 grailsApplication
的 DI .你有什么理由使用baseLink = Holders.getFlatConfig()["grails.test.base.link"]
而不是 grailsApplication.config.grails.test.base.link
?你用的是什么grails版本?
当使用 grailsApplication 作为 Controller 的依赖项时,您可以在测试中注入(inject)它:
def setup() {
myController.grailsApplication = [config: [grails: [test: [base: [link: true]]]]]
}
void "Configuration found"() {
when: ""
myController.save()
then: "Configuration found"
//some code
}
关于grails - 集成测试用例中的模拟 Grails 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986315/