我想寫單位爲我的Grails服務方法之一,單元測試就像是Grails-斯波克:打開設置方法,常用的方法
@Shared instance1, instance2, instance3
class testClass extends Specification {
def setup() {
// initaiting some value here
instance1 = new Initate(one:"one")
instance2 = new Initate(one:"one")
instance3 = new Initate(one:"one")
}
def "testmethodcall"() {
//testsetup()
when:
def result = someMethod(value1) // Here is the error
then:
result==value2
where:
value | value1 | value2
instance1 | instance2 | instance3
}
}
出於某種原因,我們打算提出這裏面的代碼設置方法的另一種方法,並打算調用它是必需的,這樣
@Shared instance1, instance2, instance3
class testClass {
def testsetup() {
// initialize the code what we initialize in setup
// initaiting some value here
instance1 = new Initate(one:"one")
instance2 = new Initate(one:"one")
instance3 = new Initate(one:"one")
}
def "testmethodcall"() {
testsetup()
when:
def result = someMethod(value1) // Here is the error
then:
result==value2
where:
value | value1 | value2
instance1 | instance2 | instance3
}
}
截至目前它是好的,方法調用工作正常,甚至變量進行了初始化,但是當我嘗試使用數據列值是返回空值,但是當我更改爲setup()
方法我得到真正的價值。有人可以解釋我,我怎麼能能夠改變setUp
方法normal method
我在這裏遇到spock的奇怪行爲 – 2014-09-24 14:08:08