Read more

RSpec: ". not_to include" behaves like ".to exclude"

Klaus Weidinger
October 11, 2021Software engineer at makandra GmbH

RSpec is smart when using the include-matcher in combination with .not_to. One could assume that

.not_to include(3, 4, 5)
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

evaluates to:

NOT( .to include(3, 4, 5) )

However, it behaves like:

.to (NOT include(3) && NOT include(4) && NOT include(5) )

Warning

Using .not_to in combination with the include-matcher doesn't logically negate the final truth value. It instead negates the individual include-expectations for each argument.

Proof

describe 'RSpec' do
  it "doesn't use logical negation for include, exhibit A" do
    expect([1, 2, 3]).to include(3, 4, 5)
  end

  it "doesn't use logical negation for include, exhibit B" do
    expect([1, 2, 3]).not_to include(3, 4, 5)
  end
end

Exhibit A fails with:

expected [1, 2, 3] to include 4 and 5

Exhibit B fails with:

expected [1, 2, 3] not to include 3

Applications

Tip

This can be used to test for exclusion of multiple values all at once.

Posted by Klaus Weidinger to makandra dev (2021-10-11 13:19)