繁体   English   中英

Spock使用表将列表设置为参数

[英]Spock set list as parameter by using table

有一个简单的类:

class Person {
   private int age;
   private String name;

   public String getName(){return this.name;}
   public int getAge(){return this.age;}
   public void setName(String name){this.name = name;}
   public void setAge(int age){this.age = age;}
 }

我有一个方法getPersonNameWithPrefix()getPeopleNames()SearchPeople在实现接口SearchPeopleImpl

class SearchPeopleImpl implements SearchPeople {

   public String getPersonNameWithPrefix(Person person){
      return "prefix" + person.getName();
   }

   public List<String> getPeopleNames(List<Person> peopleList){
      return peopleList.stream().map(Person::getName).collect(Collectors.toList());
   }

}

我想在我的测试中使用参数,它看起来像:

def 'test Person name'(){
        given:
            def searchPeople = new SearchPeopleImpl ()
            def person = Mock(Person){
                getName() >> a
            }
        when:
            def name = searchPeople.getPersonNameWithPrefix(person)
        then:
            name == b
        where:
            a         |       b
            "AA"      |       "prefixAA"
            "BB"      |       "prefixBB"
            "CC"      |       "prefixCC"
    }

它运作良好,但我测试我的第二种方法有问题。 如何将元素列表放入table (在where部分中),将其用作方法参数然后期望另一个对象列表? 我的意思是我想声明一些Person对象列表,然后检查该方法返回正确的Strings列表

@UPDATE那么有什么方法可以做以下事情:

   def 'test getting persons names'(){
        given:
            def searchPeople = new SearchPeopleImpl()
        when:
            def names = searchPeople.getPeopleNames(a)
        then:
            names == b
        where:
            a                                                                  |       b
            ["AA","BB"].collect{ x -> Mock(Person){ getName() >> x } }         |       [["AA", "BB"]]
            ["CC"].collect{ x -> Mock(Person){ getName() >> x } }              |       [["CC"]]
            ["DD","EE","FD"].collect{ x -> Mock(Person){ getName() >> x } }    |       [["DD","EE","FD"]]
    }

要么:

def 'check double2 return value'(){
    given:
        def searchPeople = new SearchPeopleImpl()
    when:
        def names = searchPeople.getPeopleNames(a)
    then:
        names == b
    where:
        people1 << [
                ["AA","BB"].collect{ x ->
                    Mock(Person){
                        getName() >> x
                    }
                }
        ]
        people2 << [
                ["CC"].collect{ x ->
                    Mock(Person){
                        getName() >> x
                    }
                }
        ]

        names1 << [["AA", "BB"]]
        names2 << [["CC"]]

        a               |       b
        people1         |       names1
        people2         |       names2
}

我只是想使用表来设置参数,但我可能完全错了。

@UPDATE有一个错误:

check double return value[0](com.test.myPlugin.api.SearchPeopleSpec)  Time elapsed: 0.125 sec  <<< FAILURE!
Condition not satisfied:

names == b
|    |  |
|    |  [[AA, BB]]
|    false
[AA, BB]

并且每行都有相同的错误。

List的实例可以在数据驱动器测试中以完全相同的方式使用。 看看下面的例子:

class PersonSpec extends Specification {

    def 'test getting persons names'() {
        given:
        def searchPeople = new SearchPeopleImpl()

        when:
        def result = searchPeople.getPeopleNames(names)

        then:
        result == expectedNames

        where:
        names << [
            ["AA", "BB"].collect { n ->
            Mock(Person) {
                getName() >> n
            }
        }
        ]
        expectedNames << [["AA", "BB"]]
    }
}

注意块中的双parens。 它必须以这种方式指定,因为如果只使用单个对,则每个参数将单独传递,而不是传递整个列表。

实际上,您可以将数据表与变量赋值( docs )组合在一起,从而提供更清晰的测试:

def 'test getting persons names'(){
    given:
    def searchPeople = new SearchPeopleImpl()

    when:
    def names = searchPeople.getPeopleNames(input)

    then:
    names == expected

    where:
    a                   |       expected
    ["AA","BB"]         |       [["AA", "BB"]]
    ["CC"]              |       [["CC"]]
    ["DD","EE","FD"]    |       [["DD","EE","FD"]]

    input = a.collect{ new Person(name: it) }
}

此外,如果您需要验证,则仅使用Mock ,例如, 1 * mock.method()如果您仅将其用1 * mock.method() ,请使用Stub来明确您的意图。 另外,当你可以通过bean构造函数构造它们时,永远不要模拟简单的POJO。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM