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

Stuff You Didn't Know You Can Do with RSpec

Stuff You Didn't Know You Can Do with RSpec

My presentation at Rails Israel on November 12, 2012

More information about custom matchers can be found on my blog post here: http://www.codelord.net/2012/02/04/extend-your-toolbox-custom-matchers/

Aviv Ben-Yosef

November 12, 2012
Tweet

More Decks by Aviv Ben-Yosef

Other Decks in Programming

Transcript

  1. PASSES ITS TESTS it “is important” do ... end it

    “is not important” do ... end
  2. PASSES ITS TESTS it “is important” do ... end xit

    “is not important” do ... end
  3. PASSES ITS TESTS it “is important”, :focus => true do

    ... end it “is not important” do ... end $ rspec -t focus
  4. PASSES ITS TESTS focused “is important” do ... end it

    “is not important” do ... end $ rspec -t focus
  5. PASSES ITS TESTS it “is pending” do pending “not implemented

    yet” do something.should be_false end end SPECS THAT SHOULDNT PASS
  6. REVEALS INTENT + PASSES ITS TESTS MATCHERS FTW The right

    matchers reveal more intent both at read time and runtime
  7. Failure: Counter.count.should == 1 expected: 1 got: 0 (using ==)

    REVEALS INTENT + PASSES ITS TESTS Counter.increment Counter.count.should == 1
  8. result should have been changed to 1, but is now

    0 REVEALS INTENT + PASSES ITS TESTS expect {Counter.increment}.to change {Counter.count}.from(0).to(1)
  9. Failure: user.cart.size.should == 2 expected: 2 got: 0 (using ==)

    REVEALS INTENT + PASSES ITS TESTS user.cart.size.should == 2
  10. expected 2 items, got 0 REVEALS INTENT + PASSES ITS

    TESTS user.cart.should have(2).items
  11. expected false to be true REVEALS INTENT + PASSES ITS

    TESTS user.active?.should be_true
  12. expected active? to return true, got false REVEALS INTENT +

    PASSES ITS TESTS user.should be_active
  13. expected false to be true REVEALS INTENT + PASSES ITS

    TESTS user.has_orders?.should be_true
  14. expected #has_orders? to return true, got false REVEALS INTENT +

    PASSES ITS TESTS user.should have_orders
  15. Mock received :add with unexpected arguments expected: (Comment :{:anonymous=>true, :user=>"dude"})

    got: (Comment :{:anonymous=>false, :user=>"dude"}) DRY + REVEALS INTENT comment = Comment.new(anonymous: true, user: "dude") commentor.should_receive(:add).with(comment)
  16. expected false to be true DRY + REVEALS INTENT commentor.should_receive(:add)

    do |comment| comment.anonymous.should be_true end
  17. Mock received :add with unexpected arguments expected: (anonymous comment) got:

    (Comment:{:anonymous=>false, :user=>"dude"}) DRY + REVEALS INTENT commentor.should_receive(:add) .with(anonymous_comment)
  18. DRY describe MyController do it “does X when user logged

    in” it “does Y when user logged in” it “doesn’t do X when not logged in” it “redirects when not logged in” end
  19. DRY describe MyController do context “logged in” do it “does

    X” it “does Y” end context “not logged in” do it “doesn’t do X” it “redirects to login” end end
  20. DRY describe MyController do it “...” do x = Foo.new(...)

    ... end it “...” do x = Foo.new(...) ... end end
  21. DRY describe MyController do it “...” do x = Foo.new(...,

    noise: ‘baz’) ... end it “...” do x = Foo.new(..., noise: ‘baz’) ... end end