Spock框架中Mock和Stub的使用与验证
1. Mock和Stub的基本关系
在测试中,Mock是Stub的超集,这意味着所有Stub的功能Mock都具备。例如下面的代码,使用Mock来实现“如果仓库为空,则无法发货”的测试:
def "If warehouse is empty nothing can be shipped"() {
given: "a basket and a TV"
Product tv = new Product(name:"bravia",price:1200,weight:18)
Basket basket = new Basket()
and:"an empty warehouse"
WarehouseInventory inventory = Mock(WarehouseInventory)
inventory.isEmpty() >> true
basket.setWarehouseInventory(inventory)
when: "user checks out the tv"
basket.addProduct tv
then: "order cannot be shipped"
!basket.canShipCompletely()
}
在Spock中,当你只需要一个具有预编程行为且不需要验证交互的假类
超级会员免费看
订阅专栏 解锁全文
4467

被折叠的 条评论
为什么被折叠?



