Grails-斯波克:打开设置方法,常用的方法
我想写单位为我的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
有几件事情需要记下: -
- 设置() - 用于与每个测试用例运行
- setupSpec() - 被用于一次一个测试类/单元
- 共享运行 - 可见throughout.And因此必须一次初始化,因此必须在当在setupSpec工作和不内设置。
- cleanup() - 与每个测试用例一起运行
- cleanupSpec - 在所有测试用例运行后的最后一次运行。
- 只有@Shared和静态变量可以从其中内访问:块。
- 单独的输入和输出与预期的双管||
除了这些,在你的代码testmethodcall方法有语法错误。
- 没有比较完成
- 期待:缺少
- 为什么Def值是存在的,而我们使用数据表变量会自动声明 此外,值2没有使用其中
而且,你说,你是能够与上面的代码成功运行测试用例时设置()方法是存在的,它只是默默地传递无失败既是值和值1为空。
下面是你的代码的修改版本
def "test methodcall"(){
setup()
expect:
value == someMethod(value1) // Here is the error
println '----instance1----'+value
println '----instance2----'+value1
println '----instance1----'+instance1
println '----instance2----'+instance2
// instance1 == instance2
where:
value | value1
instance1 | instance2
}
现在看到的println输出,你会明白这issue.Hope会得到你的答案。
感谢您的回答,无论如何是错字错误,您可以看到我的编辑问题 – 2014-09-25 10:41:32
好了!您已经使用when:then:而不是expect:now.So现在工作? – 2014-09-25 10:50:51
我更新了问题,但答案保持不变 – 2014-09-25 18:08:55
我在这里遇到spock的奇怪行为 – 2014-09-24 14:08:08