Posted about 1 month ago. Visible to the public.

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

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

.not_to include(3, 4, 5)

evaluates to:

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

However, it behaves like:

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


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.


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



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

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Klaus Weidinger
Last edit:
about 1 month ago
by Klaus Weidinger
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Klaus Weidinger to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more