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
corresponding matcher. 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
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 RSpec Matchers DSL: https://relishapp.com/rspec/rspec-expectations/docs/custom-matchers/define-a-custom-matcher 15
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 17
they: • increase indirection • don't include documentation • typically aren't tested • Store matchers within a spec/support/matchers/ directory (example: /spec/matchers/custom-matcher.rb) 18