我以为我已经理解了 Spock 的交互,但我必须管理我仍然缺少一些图片。
好吧,这是我的问题:我在 Grails 服务中有一个方法,它执行一些操作,包括调用同一服务类的 void 方法。这是代码:
class myService {
void myStuff(def myObj, params) {
// do my stuff
anotherMethod(myObj)
//do my stuff again
}
void anotherMethod(myObj) {
// do other stuff
}
}
我想确保 myStuff 方法调用另一个方法,以测试和记录正确的行为。
所以,这是我的 Spock Specification 类中的测试:
void 'test myStuff method'() {
given: 'my object and some params'
// doesn't really matter what I'm doing here
MyObject myObj = new MyObject()
params = [title: 'my object']
when: 'we call myStuff()'
service.myStuff(myObj, params)
then: 'anotherMethod() is called exactly one times'
1 * service.anotherMethod(myObj)
}
}
我得到的错误是:
Too few invocations for:
1 * service.anotherMethod(myObj) (0 invocations)
你怎么看?怎么了?
与往常一样,提前致谢。