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