Groovy 我可以在spock中为以下内容编写单元测试吗?
对! 对!Groovy 我可以在spock中为以下内容编写单元测试吗?,groovy,spock,Groovy,Spock,对! 对! 下面是另一种写作方式。我使用了数据驱动的方法,因为您必须测试多个场景(方法中有多个if) def“应接受有效的文件名”(){ 期望: accept(新文件(“/tmp”),文件名) 哪里: 下面的fileName是另一种编写方法。我使用了数据驱动的方法,因为您必须测试多个场景(方法中有多个if) def“应接受有效的文件名”(){ 期望: accept(新文件(“/tmp”),文件名) 哪里: 检查pattern的文件名不为空,然后与regex进行比较,同时检查name和extens
下面是另一种写作方式。我使用了数据驱动的方法,因为您必须测试多个场景(方法中有多个if)
def“应接受有效的文件名”(){
期望:
accept(新文件(“/tmp”),文件名)
哪里:
下面的fileName是另一种编写方法。我使用了数据驱动的方法,因为您必须测试多个场景(方法中有多个if)
def“应接受有效的文件名”(){
期望:
accept(新文件(“/tmp”),文件名)
哪里:
检查pattern
的文件名不为空,然后与regex
进行比较,同时检查name
和extension
的来源。检查pattern
的文件名不为空,然后与regex
进行比较,检查name
和extension
的来源(dir,fileName)
在这种情况下更匹配。expect:foobar.accept(dir,fileName)
在这种情况下更匹配。
public boolean accept(File directory, String fileName) {
boolean fileOK = true;
if (name != null) {
fileOK &= fileName.startsWith(name);
}
if (pattern != null) {
fileOK &= Pattern.matches(regex, fileName);
}
if (extension != null) {
fileOK &= fileName.endsWith('.' + extension);
}
return fileOK;
}
def "file should be valid"() {
setup:
def dir = new File("/tmp")
def fileName = "foo.bar"
when:
boolean valid = foobar.accept(dir, fileName)
then:
valid
}
def "should accept valid filenames"() {
expect:
foobar.accept(new File("/tmp"), fileName)
where:
fileName << ["valid_filename_1", "valid_filename_2", "valid_filename_n"]
}