Click here to load reader

Don T Mock Yourself Out Presentation

  • View
    1.741

  • Download
    3

Embed Size (px)

Text of Don T Mock Yourself Out Presentation

  • 1. Dont Mock Yourself OutDavid ChelimskyArticulated Man, Inc

2. http://martinfowler.com/articles/mocksArentStubs.html 3. Classical and Mockist Testing 4. Classical and Mockist Testing 5. Classical and Mockist Testing 6. classicist mockist 7. merbist railsist 8. rspecist testunitist 9. ist bin ein red herring 10. The big issue here is when to use amock http://martinfowler.com/articles/mocksArentStubs.html 11. agenda overview of stubs and mocks mocks/stubs applied to rails guidelines and pitfalls questions 12. test double 13. test stub describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end 14. test stub describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end 15. test stub describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end 16. test stub describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end 17. mock object describe Statement do it quot;logs a message when printedquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') logger = mock(quot;loggerquot;) statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 18. mock object describe Statement do it quot;logs a message when printedquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') logger = mock(quot;loggerquot;) statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 19. mock object describe Statement do it quot;logs a message when printedquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') logger = mock(quot;loggerquot;) statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 20. mock object describe Statement do it quot;logs a message when printedquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') logger = mock(quot;loggerquot;) statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 21. mock object describe Statement do it quot;logs a message when printedquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') logger = mock(quot;loggerquot;) statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 22. method levelconcepts 23. describe Statement do it quot;logs a message when printedquot; do customer = Object.new logger = Object.new customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 24. describe Statement do it quot;logs a message when printedquot; do customer = Object.new logger = Object.new customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 25. describe Statement do it quot;logs a message when printedquot; do customer = Object.new logger = Object.new customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 26. method stub describe Statement do it quot;logs a message when printedquot; do customer = Object.new logger = Object.new customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 27. describe Statement do it quot;logs a message when printedquot; do customer = Object.new logger = Object.new customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer, logger) logger.should_receive(:log).with(/Joe Customer/)statement.print end end 28. message expectationdescribe Statement doit quot;logs a message when printedquot; docustomer = Object.newlogger = Object.newcustomer.stub(:name).and_return('Joe Customer')statement = Statement.new(customer, logger)logger.should_receive(:log).with(/Joe Customer/) statement.printendend 29. things arent always as they seem 30. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 31. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 32. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 33. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 34. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endmessage expectation class Statement def header quot;Statement for #{@customer.name}quot; end end 35. describe Statement do it quot;uses the customer name in the headerquot; do customer = mock(quot;customerquot;) statement = Statement.new(customer) customer.should_receive(:name).and_return('Joe Customer')statement.header.should == quot;Statement for Joe Customerquot; end endbound to implementation class Statement def header quot;Statement for #{@customer.name}quot; end end 36. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 37. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 38. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 39. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end endclass Statement def header quot;Statement for #{@customer.name}quot; end end 40. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end???? class Statement def header quot;Statement for #{@customer.name}quot; end end 41. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end endmessage expectation class Statement def header quot;Statement for #{@customer.name}quot; end end 42. describe Statement do it quot;uses the customer name in the headerquot; do customer = stub(quot;customerquot;) customer.stub(:name).and_return('Joe Customer') statement = Statement.new(customer)statement.header.should == quot;Statement for Joe Customerquot; end end bound toimplementation class Statement def header quot;Statement for #{@customer.name}quot; end end 43. stubs are often used like mocks 44. mocks are often used like stubs 45. we verify stubs by checking state afteran action 46. we tell mocks to verify interactions 47. sometimes