Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 1.07 KB

File metadata and controls

39 lines (27 loc) · 1.07 KB

Avoid Double Negation With Minitest Refute

As I'm writing some tests for a recent feature, I end up with various test cases that make a variety of assertions.

assert_equal resp_body["id"], expected_id

# ...

assert_not resp_body["items"].empty?

The first assertion reads pretty naturally. The second one requires some extra mental parsing because of the _not and then the "negative" check of #empty?.

Another way to express this that might read more naturally is with #refute.

refute resp_body["items"].empty?

This says that we refute that items is empty, so the assertion should fail if empty.

Ruby is flexible in other ways. We may also prefer to write it as:

assert resp_body["items"].present?

Or we could even take advantage of a more specific variant of refute with #refute_empty:

refute_empty resp_body["items"]