Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSpec Expectations and Matchers

RSpec Expectations and Matchers

RSpec Expectations and Matchers

Stephanie Viccari

December 14, 2020
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. R pe M tc er M E s: expect(1).to eq(1)

    expect([1, 2, 3]).to include(1) expect(true).to be true 2
  2. R pe E pe ta io L 's b d

    : expect(1).to eq(1) expect(1) # accepts an argument and represents our "expectation" # create a positive expectation expect(1).to eq(1) # create a negative expectation expect(1).not_to eq(1) expect(1).to_not eq(1) 3
  3. R pe M tc er expect(1).to eq(1) eq(1) # =>

    accepts an argument and represents our "matcher" # => checks if this value matches the expectation value 4
  4. R pe E pe ta io a d at he

    s P l t r: # expect(1).to eq(1) # expectation.to(matcher) 5
  5. F eq en ly se M tc er match_array #

    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
  6. R pe P ed ca e at he s F

    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
  7. R pe P ed ca e at he s T

    s a p t m , p x h m t be_ n r h q n m . expect(user).to be_admin # calls user.admin? expect(5).not_to be_even # calls 5.even? 8
  8. R pe P ed ca e at he s I

    h p t m b t has_, p x t have_. expect(hash).to have_key(:foo) # calls hash.has_key?(:foo) 9
  9. R pe E ua it C ea S ee equal()

    # object identity # calls equal?() eql() # object equivalence # calls eql?() eq() # object equivalence with type conversion # calls == a = "my string"; b = "my string" expect(a).to equal(a) # => passes expect(a).to equal(b) # => fails a = "my string"; b = "my string" expect(a).to equal(b) # => passes expect(5).to eql(5.0) # => fails a = "my string"; b = "my string" expect(a).to equal(b) # => passes expect(5).to eq(5.0) # => passes 1
  10. M tc er - G tt C tc t em

    ll! V h c s c i : h ://r p .c /r /r -e t /d /b -i -m s D o v a f e m ? 1
  11. C n e ri e C st m at he

    ? A e ! L 's w n t r. 1
  12. C st m Sp c at he s G :

    C a m a c r a s d t h l "y". E : expect("Sunday").to end_with_y 1
  13. C st m Sp c at he : S lu

    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
  14. C st m Sp c at he Y t !

    G : C a m a c r a n g a 1 . E : expect(12).to be_greater_than_ten 1
  15. C st m Sp c at he : S lu

    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
  16. C st m Sp c at he - A gu

    en s G : C a m a c r a s d t a s c l . E : expect("kerfuffle").to end_with("e") 1
  17. C st m Sp c at he - A gu

    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
  18. C st m Sp c at he - A gu

    en Y t ! G : C a m a c r a n g a a s c n . E : expect(5).to be_greater_than(2) 2
  19. C st m Sp c at he - A gu

    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
  20. R pe M tc er - S gg st on

    4 U c m s s l t : 4 i e i t 4 d 't i d t 4 t l a 't t 4 S m s w a spec/support/matchers/ d r (e : /spec/matchers/custom-matcher.rb) 2
  21. R al or d xa pl expect(record.created_at).to be_about_now RSpec::Matchers.define :be_about_now

    do match do |actual| expect(actual).to be_within(2.seconds).of(Time.now) end end D n F B 2
  22. R so rc s R B M s: - h

    ://r p .c /r /r -e t /d / b -i -m s R D n a C M - h ://r p .c /r /r -e t /d / c -m s/d n -a-c -m 2