Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mocking & Stubbing

422 views

Published on

A quick look at Mocking & Stubbing in RSpec 3.2.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Mocking & Stubbing

  1. 1. Mocking & Stubbing FOR GREAT JUSTICE!
  2. 2. It’s About Context
  3. 3. Don’t Get It Wrong
  4. 4. For Additional Reading • Dependency Inversion Principle • Liskov Substitution Principle • Dependency Injection • Law of Demeter • “Tell, Don’t Ask”
  5. 5. Mocking Creating Fake Things
  6. 6. Mocking book = double("book")
 book = instance_double("Book", :pages => 250) notifier = class_double("ConsoleNotifier") user = object_double(User.new, :save => true)
  7. 7. Mocking http://www.relishapp.com/rspec/rspec-mocks/docs
  8. 8. Stubbing Doing Fake Things
  9. 9. Stubbing dbl = double("Some Collaborator") allow(dbl).to receive(:foo) expect(dbl).to receive(:foo)
  10. 10. Stubbing dbl = double("Some Collaborator") allow(dbl).to receive(:foo) {…} expect(dbl).to receive(:foo) {…}
  11. 11. Stubbing dbl = double("Some Collaborator") allow(dbl).to receive(:foo).with(some_args) {…} expect(dbl).to receive(:foo).with(some_args) {…}
  12. 12. Stubbing http://www.relishapp.com/rspec/rspec-mocks/docs
  13. 13. Mocking & Stubbing It’s Dangerous…It’s Difficult…But…
  14. 14. Some Advice Ignore At Your Peril
  15. 15. Some Advice • allow_any_instance_of is an almost sure sign you’re doing it wrong. • expect_any_instance_of is the same. • If you create more than a couple doubles you’ve probably found something with a high degree of coupling • If you create more than a couple stubs per discreet test, you’re probably asking for too much • Some kinds of classes will almost always exhibit a high degree of coupling — View/Form Objects — Everything else is definitely a code smell

×