question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why does shouldHaveSingleElement and other Collection "shoulds" have predicate instead of block for assertions?

See original GitHub issue

What I do not really get is: when does your library want predicates (T)->Boolean and when does it require a function block that itself can do multiple assertions using matchers and should constructs?

I.e.:

list.shouldHaveSingleElement {
   it.name == "Bob"
}

vs. (not the case)

list.shouldHaveSingleElement {
  it.name shouldBe "Bob"
}

I’ve stumbled across both ways, but the simple predicate one is used for collections - and I do not like it at all, because the error messages don’t reveal any reason for failing the match whereas the latter one could state that something was expected to be “Bob” but is “Alice” for example.

Is there a reason behind this that I cannot see?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
sksamuelcommented, Nov 16, 2021

The std lib has the method .single() to extract only a single element from a list, and throw if the list does not have size 1.

0reactions
sksamuelcommented, Nov 22, 2021

I think the difference here, is that they are single predicates. So each predicate is executed in turn on the next element of the list.

With inspectors, everything in the block is executed every each element, and the counts then compared to the requirements.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Core Matchers | Kotest
shouldBe(other), General purpose assertion that the given obj and other are both equal. expr.shouldBeTrue(), Convenience assertion that the expression is ...
Read more >
Use io.kotest.matchers.collections.nulls.Collection ... - LambdaTest
Learn how to use Collection.shouldContainNoNulls method in io.kotest.matchers.collections.nulls for your next Kotest project with LambdaTest Automation ...
Read more >
Unit 2 Lab 3: Making Decisions by Using Predicates, Page 1
Predicates are reporter blocks (functions) that always report a Boolean value (they report only the values true or false ). In Snap!, they...
Read more >
Advanced googletest Topics
Even though googletest has a rich set of assertions, they can never be ... you can instead use the following predicate-formatter assertions to...
Read more >
https kotest io docs assertions inspectors html Inspectors | Kotest ...
Inspectors have sort of eluded me.. My typical use-pattern when writing tests is to write my test and go x should... and look...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found