2017-10-09 90 views
1

有一个简单的类:斯波克设置列表作为参数通过使用表

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

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

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] 

,并没有为每个行同样的错误。

+0

模拟简单的pojos是不好的做法,通过使用bean构造函数,例如'[“AA”,“BB”],collect {new Person(name:it)}',可以达到同样的效果。 –

回答

2

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。它必须以这种方式指定,因为如果只使用单个对,则每个单独的参数将被单独传递而不是传递整个列表。

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()如果你只使用它:

+0

我编辑了我的文章,请问您能检查一下吗? –

+0

@tombobby,你的第一个例子运行良好,当使用这个语法时,'b'中不需要额外的parens,只需使用single。第二个例子是由设计无效的两种方法的组合。 – Opal

+0

不幸的是第一个解决方案不起作用(我再次编辑我的文章,并把它放在那里) –

1

实际上,你可以用它提供了一个更清洁的测试变量赋值(docs)合并数据表作为合作者使用Stub而不是使你的意图清晰。另外,当你可以通过bean构造函数构造它们时,决不要模拟简单的POJO。

+0

你能举个例子怎么把一些值放到对象上吗?在这个例子中,如何把'name'(如你所做的)和'age'放到每个对象上? –

+0

@tombobby我不明白你想要什么?你在问这个问题:http://groovy-lang.org/style-guide.html#_initializing_beans_with_named_pa​​rameters_and_the_default_constructor你可以这样做:[[name:“AA”,age:18],[name:“BB”,年龄:20]]。收集{新人(它)}' –