Proposal to use eq(true/false) istead of truthy and falsey in tests
With the issues discovered #230603 (closed) we want to fix all usages of truthy and falsey in tests
When thinking to use truthy and falsey we general want to check for a boolean value, this build in matchers from rspec can be misleading and are causing possible unwanted side-effects
https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
Alternatives:
be(true)eq(true)eql(true)
Equality matchers https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers/equality-matchers
RSpec.describe "a string" do
it "is equal to another string of the same value" do
expect("this string").to eq("this string")
end
it "is not equal to another string of a different value" do
expect("this string").not_to eq("a different string")
end
end
RSpec.describe "an integer" do
it "is equal to a float of the same value" do
expect(5).to eq(5.0)
end
end
RSpec.describe "an integer" do
it "is equal to another integer of the same value" do
expect(5).to eql(5)
end
it "is not equal to another integer of a different value" do
expect(5).not_to eql(6)
end
it "is not equal to a float of the same value" do
expect(5).not_to eql(5.0)
end
end
Proposal
Update tests to use eq(true) eq(false) and update docs to reflect this
Next steps:
-
Update Testing best practices to reflect the method we use -
Update tests under /spec to use eq(true) eq(false) -
Update tests under ee/spec to use eq(true) eq(false) -
Enable Rubocop rule #230685 (closed)
Edited by Alina Mihaila