断言列表包含 Spock 中给定 class 的元素
assert list contains elements of given class in Spock
我有一个包含一些处理程序的链管理器。我想检查它是否默认具有特定 classes 的处理程序。怎么做?
def "contains few updaters on default"(){
when:
def manager = new UpdateManager();
then:
manager.getUpdaters().size() == 2;
//how to check that the list contains elements of classes
//OneThingUpdater and OtherThingUpdater
}
编辑:
我对Answer的代码进行了改进,以防御相同class的两个元素,所以现在是这样的:
def "contains few updaters on default"(){
setup:
def expectedUpdaters = [OneThingUpdater, OtherThingUpdater]
when:
def manager = new UpdateManager();
then:
def list = manager.getUpdaters()
list.size() == 2;
list.every {
it.class in expectedUpdaters
expectedUpdaters.remove(it.class)
}
}
def "contains few updaters on default"(){
when:
def manager = new UpdateManager()
then:
def list = manager.getUpdaters()
list.size() == 2
list*.getClass().every { it in [OneThingUpdater, OtherThingUpdater] }
// or, just this
// Assuming the classes do not implement Map interface
// getClass() can be replaced with class
list*.class == [OneThingUpdater, OtherThingUpdater]
}
我有一个包含一些处理程序的链管理器。我想检查它是否默认具有特定 classes 的处理程序。怎么做?
def "contains few updaters on default"(){
when:
def manager = new UpdateManager();
then:
manager.getUpdaters().size() == 2;
//how to check that the list contains elements of classes
//OneThingUpdater and OtherThingUpdater
}
编辑: 我对Answer的代码进行了改进,以防御相同class的两个元素,所以现在是这样的:
def "contains few updaters on default"(){
setup:
def expectedUpdaters = [OneThingUpdater, OtherThingUpdater]
when:
def manager = new UpdateManager();
then:
def list = manager.getUpdaters()
list.size() == 2;
list.every {
it.class in expectedUpdaters
expectedUpdaters.remove(it.class)
}
}
def "contains few updaters on default"(){
when:
def manager = new UpdateManager()
then:
def list = manager.getUpdaters()
list.size() == 2
list*.getClass().every { it in [OneThingUpdater, OtherThingUpdater] }
// or, just this
// Assuming the classes do not implement Map interface
// getClass() can be replaced with class
list*.class == [OneThingUpdater, OtherThingUpdater]
}