Support matching elements in arrays ignoring fields
See original GitHub issueHello Kotest developers!
I was wondering if you were planning on developing a feature for assertions to match list elements by ignoring fields. Here is a sample of that kind of assertings with assertJ
assertThat(underTest.findAll())
.usingElementComparatorIgnoringFields(ConfiguredTask::id.name)
.containsExactlyInAnyOrderElementsOf(expectedTasks)
Maybe something similar could be implemented in Kotest?
Thank you!
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (10 by maintainers)
Top Results From Across the Web
Testing content of list ignoring some of the fields - java
I have a scenario where I receive a list from a method call and I would like to assert that the list contains...
Read more >How to ignore empty fields, objects, and arrays using ...
Using the below dataweave function, you can ignore any empty values, objects or arrays that you want! You can update the case v...
Read more >Array methods - The Modern JavaScript Tutorial
sort() sorts the array in place, changing its element order. It also returns the sorted array, but the returned value is usually ignored,...
Read more >array_intersect - Manual - PHP
array_intersect handles duplicate items in arrays differently. If there are duplicates in the first array, all matching duplicates will be returned.
Read more >Work with arrays | BigQuery - Google Cloud
You can also flatten ARRAY type fields of STRUCT values. Querying STRUCT elements in an ARRAY. The following example uses UNNEST with CROSS...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this is an excellent idea. And we can provide a few comparators out of the box.
I have an idea of how it could work. Right now, Kotest has a few matchers with long names. So to follow library naming, the function should be something like
That said this becomes really hard to read, and you need to provide overloads for the different configurations (one without
ignorePrivateFiels
for instance.What could be great is to pass an optional object
comparor
. The default comparator would simply do an object equality, which is the current behaviour (using==
).Something like this:
You could therefore, if you want to verify the same thing ignoring fields, do something like this:
I understand this is a rather complex concept to implement, and that it should probably not be part of a single merge request to implement this feature, but this is the best way I personally see this could be implemented.
Also, this approach could maybe help reduce the number of functions in the library and allow for more powerful matching.
Let me know what you think about the idea 😃