confirm two arrays match, regardless of ordering expect(["apricots", "mango"]).to match_array(["mango", "apricots"]) --- include # confirm a collection includes one or more expected values. expect(a: 1, b: 2).to include(b: 2) expect(a: 1, b: 2).not_to include(c: 3, d: 4) --- match # confirm a string matches a regex expect(welcome_email.body).to match(/Welcome #{name}/) 6
n p t m , R p s o n m . expect([].empty?).to eq(true) # this use of an equality matcher works but with # predicate matchers, we can make this more readable. expect([]).to be_empty 7
io # spec/custom_matcher_spec.rb RSpec::Matchers.define :end_with_y do # actual represents the value being examined # (aka, the value passed to `expect`) match do |actual| # `match` expects a block that returns a boolean value actual[-1] == "y" end end RSpec.describe "custom matcher" do it "returns true given a string that ends with the letter 'y'" do expect("Sunday").to end_with_y expect("sunshine").not_to end_with_y end end R M s D : h ://r p .c /r /r -e t /d /c -m s/d n -a-c -m 1
io # spec/greater_than_ten_matcher_spec.rb RSpec::Matchers.define :greater_than_10 do match do |actual| actual > 10 end end RSpec.describe "custom matcher" do it "returns true given a number greater than 10" do expect(20).to be_greater_than_ten expect(7).not_to be_greater_than_ten end end 1
en s: S lu io # spec/end_with_matcher_spec.rb RSpec::Matchers.define :end_with do |matcher| match do |actual| actual[-1] == expected end end RSpec.describe "custom matcher" do it "returns true when the string ends with the specified letter" do expect("kerfuffle").to end_with("e") expect("kerfuffle").not_to end_with("f") end end 1
en : S lu io # spec/be_greater_than_matcher_spec.rb RSpec::Matchers.define :be_greater_than do |expected| match do |actual| actual > expected end end RSpec.describe "custom matcher" do it "returns true when the value is greater than the provided number" do expect(5).to be_greater_than(2) expect(5).not_to be_greater_than(10) end end 2