【发布时间】:2013-12-27 21:39:37
【问题描述】:
这个问题是这个问答的一个分支:Test Groovy class that uses System.console()
问题:
Spock 框架错误地使用可变参数检查模拟方法的参数。
重现步骤:
1) 创建 groovy 项目
2) 创建接口:
interface IConsole {
String readLine(String fmt, Object ... args)
}
3) 创建 spock 测试:
class TestInputMethods extends Specification {
def 'test console input'() {
setup:
def consoleMock = GroovyMock(IConsole)
1 * consoleMock.readLine(_) >> 'validResponse'
when:
// here we get exception "wrong number of arguments":
def testResult = consoleMock.readLine('testPrompt')
then:
testResult == 'validResponse'
}
}
4) 尝试运行测试
效果:测试失败,异常“参数数量错误”。
解决方法:
对 readLine 的调用被替换为:
def testResult = consoleMock.readLine('testPrompt', [] as Object[])
然后测试成功完成。
问题:
有没有更好的方法来“解释”spock 模拟函数的最后一个参数是可变参数,因此可以省略?
【问题讨论】:
标签: unit-testing groovy mocking spock