【发布时间】:2016-11-05 21:26:29
【问题描述】:
我有以下测试(这可能更像是一个功能测试而不是集成但是......):
@Integration(applicationClass = Application)
@Rollback
class ConventionControllerIntegrationSpec extends Specification {
RestBuilder rest = new RestBuilder()
String url
def setup() {
url = "http://localhost:${serverPort}/api/admin/organizations/${Organization.first().id}/conventions"
}
def cleanup() {
}
void "test update convention"() {
given:
Convention convention = Convention.first()
when:
RestResponse response = rest.put("${url}/${convention.id}") {
contentType "application/json"
json {
name = "New Name"
}
}
then:
response.status == HttpStatus.OK.value()
Convention.findByName("New Name").id == convention.id
Convention.findByName("New Name").name == "New Name"
}
}
数据正在通过 BootStrap 加载(这可能是问题所在),但问题是当我在 then 块中时;它通过新名称找到Convention 和id 匹配,但是在测试name 字段时,它失败了,因为它仍然具有旧名称。
在阅读有关测试的文档时,我认为问题在于数据是在哪个会话中创建的。由于@Rollback 的会话与BootStrap 是分开的,因此数据并没有真正凝胶化。例如,如果我通过测试的given 块加载数据,那么当RestBuilder 调用我的控制器时,该数据不存在。
完全有可能我不应该以这种方式进行这种测试,因此我们非常感谢您的建议。
【问题讨论】:
标签: grails integration-testing functional-testing grails3