Saya punya 2 json sebagai berikut:

* def first = [{"code":"00","name":"A","iconUrl":"https:env1.test.png"}, {"code":"01","name":"B"}, {"code":"03","name":"C","iconUrl":"https:env1.test.png"}, {"code":"02","name":"D"}]

* def second = [{"iconUrl":"https:env2.test.png", "code":"00","name":"A"}, {"code":"01","name":"B"}, {"iconUrl":"https:env2.test.png", "code":"03","name":"C"}, {"code":"02","name":"D"}]

Saya ingin membandingkan 2 objek json. Semua objek tidak memiliki iconUrl dan yang memiliki perbedaan env di url.

Saya mencoba yang berikut ini:

* def first = [{"code":"00","name":"A","iconUrl":"https:env1.test.png"}, {"code":"01","name":"B"}, {"code":"03","name":"C","iconUrl":"https:env1.test.png"}, {"code":"02","name":"D"}]
    * def second = [{"iconUrl":"https:env2.test.png", "code":"00","name":"A"}, {"code":"01","name":"B"}, {"iconUrl":"https:env2.test.png", "code":"03","name":"C"}, {"code":"02","name":"D"}]
    * def size = second.length
    And print "     size: ", size
    * def fun = function(i){ if (second[i].iconUrl != '#null') second[i].iconUrl = '#string'}
    * def foo = karate.repeat(size, fun)
    * match first contains second

Pengujian saya gagal karena objek array tidak mengandung iconUrl. Bagaimana cara menambahkan kondisi lain untuk objek yang tidak mengandung iconUrl?

1
user1873274 3 Juli 2020, 09:46

1 menjawab

Jawaban Terbaik

Ini dia:

* def fun = function(x){ if (x.iconUrl) x.iconUrl = x.iconUrl.replace('env2', 'env1'); return x }
* def secondFixed = karate.map(second, fun)
* match first == secondFixed
0
Peter Thomas 3 Juli 2020, 07:02