gpt4 book ai didi

unit-testing - Rspec的let的Grails等效于什么?

转载 作者:行者123 更新时间:2023-12-02 15:19:08 25 4
gpt4 key购买 nike

来自Rails,在Grails中进行单元测试非常困难。在Grails中,以下内容等同于什么?
let(:some_variable) { SomeObject.new(name: 'Blabla', ...) }
我想定义一些可以在每个测试中使用的变量。此外,我希望保存对象,例如:

def "some test"() {
// given variable1 and variable2

then:
response.json.size() == 2
}

因此,测试将具有两个变量,并且响应将产生这两个对象的数组,大小为2。

最佳答案

简单的情况:

@TestFor(SomeController)
class SomeControllerSpec extends Specification {
def "some test"() {
given:
def var1 = new SomeObject(name:"AAA").save(flush:true)
def var2 = new SomeObject(name:"BBB").save(flush:true)
when:
controller.someAction()
// assuming someAction fetches the SomeObject instances
// and marshals a JSON Response
then:
controller.response.json.size() == 2
}
}

也许:
@TestFor(SomeController)
class SomeControllerSpec extends Specification {

// var1 and var2 are saved to the DB, and persist across all
// the tests in this specification
def var1 = new SomeObject(name:"AAA").save(flush:true)
def var2 = new SomeObject(name:"BBB").save(flush:true)

def "some test"() {
when:
controller.someAction()
then:
controller.response.json.size() == 2
}
}

关于unit-testing - Rspec的let的Grails等效于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774297/

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