Rspec group

363 views

Published on

Short discussion on Mocks and Stubs for RSpec study group. Done by thefonso.

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

  • Be the first to like this

Rspec group

  1. 1. Review TerminologyThursday, August 9, 12
  2. 2. Mocks and Stubs http://rubydoc.info/gems/rspec-mocks/ • A Method Stub returns a pre-determined value. • A Mock Object is a Test Double that supports message expectations and method stubs • Why should you care?Thursday, August 9, 12
  3. 3. Expectations http://rubydoc.info/gems/rspec-expectations/RSpec/Expectations • should • should_not • should_receive • should_not_receive • expectThursday, August 9, 12
  4. 4. Method Stubs http://rubydoc.info/gems/rspec-mocks/ • A method stub returns a pre-determined value. • rspec-mocks supports 3 forms for declaring method stubs: book.stub(:title) { "The RSpec Book" } book.stub(:title => "The RSpec Book") book.stub(:title).and_return("The RSpec Book")Thursday, August 9, 12
  5. 5. Mocks http://rubydoc.info/gems/rspec-mocks/ • A Mock Object is a Test Double that supports message expectations and method stubs • Mock Object example...Thursday, August 9, 12
  6. 6. class Zombie < ActiveRecord::Base has_one :weapon def decapitate weapon.slice(self, :head) self.status = "dead again" end endThursday, August 9, 12
  7. 7. describe Zombie do let(:zombie) { Zombie.create } context "#decapitate" do it "calls weapon.slice" do zombie.weapon.should_receive(:slice) zombie.decapitate end it "sets status to dead again" do zombie.weapon.stub(:slice) zombie.decapitate zombie.status.should == "dead again" end end endThursday, August 9, 12
  8. 8. Exercise Use resource links and write some testThursday, August 9, 12
  9. 9. Resources: Book: • http://www.amazon.com/The-RSpec-Book-Behaviour-Development Online: • http://rubydoc.info/gems/rspec-expectations/RSpec/Expectations • http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers • http://rubydoc.info/gems/rspec-mocks/Thursday, August 9, 12

×