用复杂的对象进行排序

时间:2018-02-27 19:18:03

标签: typescript chai

我正在使用chai-sorted进行测试。 我需要测试数组是否针对属性正确排序

user.firstname

我的chai-sorted指令是

response.should.be.sortedBy('user.firstname');

但是柴是错误的

AssertionError: expected '[null,null,null,null,null,null]' to be sorted in ascending order
at Object.<anonymous> (build/test/e2e/http/applicants-api.js:591:38)
at Generator.next (<anonymous>)
at fulfilled (build/test/e2e/http/applicants-api.js:4:58)
at process._tickCallback (internal/process/next_tick.js:109:7)

如何在二级属性上指定排序?

1 个答案:

答案 0 :(得分:1)

这是不可能的,因为chai-sorted会从这样的项中获取密钥:item[key]因此在您的示例中,item['user.firstname']不起作用。你可以这样做:

response.map(r => r.user).should.by.sortedBy('firstname')

当然不如你那么好。