我在小 cucumber 中有一个类似的步骤:
And the date range options are correct
| startDate | endDate | isDefault | isCurrent |
| 2020-03-02 | 2021-03-01 | false | false |
| 2019-03-02 | 2020-03-01 | false | false |
在steps definition类中,我尝试使用rest assured和hamcrest来验证json响应中的日期范围。
@And("the date range options are correct")
public void validateDateRanges(DataTable expectedDateRanges) {
String response = getJSONResponse();
List<Map<String,Object>> expected = expectedDateRanges.asMaps(String.class, Object.class);
assertThat("Date Ranges are correct", from(response).get("dateRangeOptions"), equalTo(expected));
}
但是,此操作失败,并出现以下错误:
Expected: <[{startDate=2020-03-02, endDate=2021-03-01, isDefault=false, isCurrent=false}, {startDate=2019-03-02, endDate=2020-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2019-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2018-06-17, isDefault=true, isCurrent=false}, {startDate=2017-03-02, endDate=2018-03-01, isDefault=false, isCurrent=false}, {startDate=2016-03-02, endDate=2017-03-01, isDefault=false, isCurrent=false}, {startDate=2015-08-18, endDate=2016-03-01, isDefault=false, isCurrent=false}]>
but: was <[{startDate=2020-03-02, endDate=2021-03-01, isDefault=false, isCurrent=false}, {startDate=2019-03-02, endDate=2020-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2019-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2018-06-17, isDefault=true, isCurrent=false}, {startDate=2017-03-02, endDate=2018-03-01, isDefault=false, isCurrent=false}, {startDate=2016-03-02, endDate=2017-03-01, isDefault=false, isCurrent=false}, {startDate=2015-08-18, endDate=2016-03-01, isDefault=false, isCurrent=false}]>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at scenarios.pagination.AccrualLog.validateDateRanges(AccrualLog.java:80)
at ✽.the date range options are correct(file:///.../accrualLog.feature:7)
考虑到这些值看起来都是正确的,我认为这是由于数据类型不匹配造成的-布尔字段作为字符串从datatable返回。
除了使用类来表示日期范围之外,还有什么方法可以解决这个问题吗?
1条答案
按热度按时间dldeef671#
我找到了解决办法。最后,我创建了自己的matcher实现来检查cucumber数据表:
我可以这样使用它:
这很管用。不过,我很想知道是否有更优雅的解决方案。