Rspec 101


Published on

Intro to RSpec methods

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Thebe_XXXX test works only if the item you called should on has a XXXX? method.
  • If the should method doesn’t have an explicit receiver, it will delegate to the declared subject.Read your tests out loud to make sure they sound right. “Specify the subject should be eligible to vote” vs “it should be eligible to vote”.
  • should_receive will error if :name is not calledstub will not error if :occupation is not called
  • Rspec 101

    1. 1. Rspec 101<br />Jason Noble<br /><br />
    2. 2. Example Group<br />describe()<br />Defines example group of tests<br />String we pass describes the item we’re testing<br />it()<br />Defines a code example<br />String we pass describes the specific behaviour<br />
    3. 3. describe() Method<br />describe “A User” {…}<br />A User<br />describe User {…}<br />User<br />describe User, “with no roles assigned” {…}<br />User with no roles assigned<br />
    4. 4. Describe blocks can be nested<br />
    5. 5. context() method<br />context() is an alias for describe()<br />Use describe() for things, context() for context<br />
    6. 6. it() method<br />Argument should state what is being tested<br />
    7. 7. Pending tests<br />We can mark tests to be implemented “later”<br />
    8. 8. Pending tests (cont.)<br />Each method of marking a test as pending has its usefulness:<br />Add pending examples as you think of stuff to test<br />Disable failing examples without losing track that you need to fix those at some point<br />Wrap failing examples when you want to be notified when changes to the system cause them to pass (bug is fixed, etc)<br />
    9. 9. before()/after() method<br />Before/after methods helps you set and/or reset initial state<br />Create a new stack, add one element to it<br />Takes one argument<br />:each<br />Executes this block before each test group executes<br />:all<br />Executes this block once for all tests before the first test is run<br />
    10. 10. before(:each) method<br />
    11. 11. before(:all)<br />Method is run once and only once for a group of tests<br />Be careful using this method, usually we want each test to have it’s own environment setup<br />Sharing state between examples can cause unexpected things<br />Good examples:<br />Opening a network connection<br />Pre-seeding caches<br />
    12. 12. after(:each) method<br />Code is ran after each example<br />Rarely necessary because each example runs in its own scope, and consequently the instance variables in that scope are reset<br />Can be useful to reset global state of things after your test completes<br />after(:each) is guaranteed to run after each example, even if failure or errors are raised<br />
    13. 13. after(:each) example<br />
    14. 14. after(:all) method<br />This is even more rare than the after(:each)<br />Examples:<br />Close down browsers<br />Close database connections<br />Close sockets<br />Any resource we want to release when we’re done, but not after every individual test<br />
    15. 15. around(:each) method<br />Supports APIs that require a block<br />Very rarely, if ever used<br />I have never used this<br />Put your functionality into before/after blocks if at all possible<br />See if you’re interested<br />
    16. 16. Helper Methods<br />Defined within an example group<br />Available to all examples in the group<br />
    17. 17. Shared Helper Methods<br />If helper methods need to be used across example groups, put them in one or more modules and include modules in example groups we want to have access<br />
    18. 18. Shared Examples<br />If we expect instances of more than one class to behave in the same way, a shared example group describes the behavior once and includes it in multiple example groups<br />
    19. 19. Shared examples (cont.)<br />
    20. 20. RSpec::Expectations<br />One goal of BDD is getting the words right<br />Expectations vs. Assertions<br />We are setting an expectation of what should happen rather than what will happen<br />In fact the word should is part of RSpec<br />result.should equal(5)<br />message.should match(/on Sunday/)<br />
    21. 21. should, should_not and matchers<br />result.should equal(5)<br />If result is equal to 5, it passes<br />result.should_not equal(5)<br />If result is anything other than 5, it passes<br />General Pattern:result.should ________(value)<br />_______ is a matcher<br />
    22. 22. RSpec built in Matchers<br />include(item)<br />prime_numbers.should_not include(8)<br />respond_to(message)<br />list.shouldrespond_to(:length)<br />raise_error(type)<br />lambda {! }.should raise_error(NameError)<br />
    23. 23. 4 ways to be equal<br />a == b<br />Value equality (Most common)<br />a === b<br />Is Object a the same Object as b<br />a.eql?(b)<br />Are a and b values equal and of same type<br />a.equal?(b)<br />Is Object a the same Object as b<br />(General Rule: The longer the method name, the more restrictive the matcher is)<br />
    24. 24. Do not use != in expectations<br />actual.should != expected<br />action.should_not == expected<br />Causes issues, explained in detail in the RSpec book<br />
    25. 25. Floating Point Calculations<br />“expected 5.25 got 5.251” is frustrating in a failure message<br />RSpec offers a be_close matcher that accepts an expected value and an acceptable delta<br />result.shouldbe_close(5.25, 0.005)<br />Will pass as long as result is within .005 of 5.25<br />
    26. 26. Matching Text<br />response.should match(/this expression/)<br />Matches if response has text “this expression” somewhere in its contents<br />response.should =~ /this expression/<br />Functionally equivalent to the previous one<br />
    27. 27. Expect{}<br />Tests that a block of code causes some effect<br />You can also use .to(1) or .from(0).to(1)<br />
    28. 28. Predicate Matchers<br />How do we test array.empty?<br />array.empty?.should == true<br />array.shouldbe_empty<br />
    29. 29. Predicate matchers (cont.)<br />RSpec gives you other options<br />be_<br />be_a<br />ted.shouldbe_a_kind_of(Player) => ted.kind_of?(Player)<br />be_an<br />ted.shouldbe_an_instance_of(Player) =>ted.instance_of?(Player)<br />RSpec also lets you write your own matchers<br />
    30. 30. Matchers<br />have_ becomes has_<br />request_params.shouldhave_key(:id) =>request_params.has_key?(:id).should == true<br />
    31. 31. specify{}<br />Sometimes RSpec can guess your tests<br />it “should have 32 pieces” do @board.should have(32).piecesend<br />specify { @board.should have(32).pieces }<br />This is used rarely (very simple tests)<br />
    32. 32. subject {}<br />Clean up your tests by specifying a subject<br />
    33. 33. RSpec Mocks<br />Mocks allow you to fake functionality that isn’t being tested. See the book for more info.<br />
    34. 34. rspec command<br />rspec –help<br />List options available to running RSpec<br />rspec spec/simple_math_spec.rb<br />Run only one spec file<br />rspec spec<br />Run all specs in spec/ directory<br />rspec spec --format documentation<br />Makes RSpec more verbose with test output<br />
    35. 35. rspec commands (cont.)<br />rspec spec –color<br />Passing is green, pending is yellow, fail is red<br />Store common options in .rspec file<br />--color<br />--format documentation<br />Options stored in ./.rspec take precedence over ~/.rspec, options declared command line win<br />
    36. 36. Let’s get started<br />mkdir -p calculator/{lib,spec}<br />cd calculator<br />mate .<br />
    37. 37. spec/calculator_spec.rb<br />lib/calculator.rb<br />
    38. 38. spec/calculator_spec.rb<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.