Jan*_*omä 15 groovy unit-testing spock
我想用mock模拟一些流畅的界面,这基本上是一个邮件生成器:
this.builder()
.from(from)
.to(to)
.cc(cc)
.bcc(bcc)
.template(templateId, templateParameter)
.send();
Run Code Online (Sandbox Code Playgroud)
使用Spock进行模拟时,需要进行大量的设置:
def builder = Mock(Builder)
builder.from(_) >> builder
builder.to(_) >> builder
Run Code Online (Sandbox Code Playgroud)
当你想根据用例测试与模拟的某些交互时,它变得更加麻烦.所以我在这里基本上有两个问题:
有没有办法用较少的代码指定一个流畅的接口的模拟,例如:
def builder = Mock(Builder)builder./(from|to|cc|bcc |template)/(*) >> builder
或类似于Mockito的Deep Stubs的东西(参见http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#RETURNS_DEEP_STUBS)
Pet*_*ser 16
你可以这样做:
def "stubbing and mocking a builder"() {
def builder = Mock(Builder)
// could also put this into a setup method
builder./from|to|cc|bcc|template|send/(*_) >> builder
when:
// exercise code that uses builder
then:
// interactions in then-block override any other interactions
// note that you have to repeat the stubbing
1 * builder.to("fred") >> builder
}
Run Code Online (Sandbox Code Playgroud)