Posted 8 months 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
Copy.not_to include(3, 4, 5)
evaluates to:
CopyNOT( .to include(3, 4, 5) )
However, it behaves like:
Copy.to (NOT include(3) && NOT include(4) && NOT include(5) )
Warning
Using
.not_to
in combination with theinclude
-matcher doesn't logically negate the final truth value. It instead negates the individual include-expectations for each argument.
Proof
Copydescribe '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:
Copyexpected [1, 2, 3] to include 4 and 5
Exhibit B fails with:
Copyexpected [1, 2, 3] not to include 3
Applications
Tip
This can be used to test for exclusion of multiple values all at once.
Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.