【发布时间】:2020-11-19 21:58:42
【问题描述】:
为了提取数据表值以在 Spock 的报告扩展中使用,我使用 以下代码:
@Override
public void beforeIteration(IterationInfo iteration) {
Object[] values = iteration.getDataValues();
}
这将返回给我对数据表中对象的引用。但是,我想得到 引用该值的变量的名称。
例如,在下面的测试中:
private static User userAge15 = instantiateUserByAge(15);
private static User userAge18 = instantiateUserByAge(18);
private static User userAge19 = instantiateUserByAge(19);
private static User userAge40 = instantiateUserByAge(40);
def "Should show popup if user is 18 or under"(User user, Boolean shouldShowPopup) {
given: "the user <user>"
when: "the user do whatever"
...something here...
then: "the popup is shown is <showPopup>"
showPopup == shouldShowPopup
where:
user | shouldShowPopup
userAge15 | true
userAge18 | true
userAge19 | false
userAge40 | false
}
有没有办法接收字符串“userAge15”、“userAge18”、“userAge19”、“userAge40”而不是它们的值?
这样做的动机是对象 User 很复杂,包含大量信息,如姓名、姓氏等,其 toString() 方法会使我生成的报告中的 where 子句不可读。
【问题讨论】: