我可以在spock中编写单元测试吗?

时间:2012-05-14 19:20:05

标签: groovy spock

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;
}

2 个答案:

答案 0 :(得分:5)

下面是另一种写作方式。我使用数据驱动的方法,因为你必须测试多个场景(方法中有多个if)

def "should accept valid filenames"() {
    expect:
    foobar.accept(new File("/tmp"), fileName)

    where:
    fileName << ["valid_filename_1", "valid_filename_2", "valid_filename_n"]
}

答案 1 :(得分:1)

是!

def "file should be valid"() {
    setup:
        def dir = new File("/tmp")
        def fileName = "foo.bar"

    when:
        boolean valid = foobar.accept(dir, fileName)

    then:
        valid
}