Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 8/12/2009: Using stub with before(:all)

Glenn Jahnke
Wednesday, August 12, 2009

Ask for Help

“My method stub only works on the first RSpec example and clearing in subsequent examples, what’s wrong?”

Using RSpec, it is common to mock out certain aspects of your code to change functionality for testing. This is accomplished using the “stub” method and passing the method name as a symbol.

SomeObject.stub(:some_method_to_modify).and_return do something_else() end

As we like to keep our code nice and DRY, we often pull things into “before” blocks. Unfortunately, this can cause some confusion as

describe "ObjectA" do
    before :all do
        ObjectB.stub!(:some_method_ObjectA_depends_on).and_return(15)
    end

    it "can test something" do
        ...
    end

    it "can test something else" do
        Objectb.some_method_ObjectA_depends_on
    end

end

will have ObjectB.some_method_ObjectA_depends_on actually executing ObjectB’s method instead of stubbing. This is because after each example (”it” block), all stubs are cleared from all objects, leaving the stubs only effective in the first example.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. Chad Woolley says:

    Why aren’t you using `’before :each’` (which is the no-param default for `’before’`) instead of `’before :all’`? This behavior makes sense to me – mocks must be cleared out after each example; having stubs do the same is logical.

    You can also have both `’before :all’` and `’before :each’` blocks in your specs (see the [rspec docs](http://rspec.info/documentation/)), so any mocking/stubbing should be done in a `’before :each’`.

    Out of curiosity, why did you need `’before :all’`? I don’t think I’ve ever needed to use it…

    August 14, 2009 at 2:00 am

  2. Glenn Jahnke says:

    @Chad, I don’t know why the person at standup was using them, but perhaps they were doing some expensive but necessary test setup like downloading a file or doing a complex database query that must happen before the tests, but only once because the data is not modified.

    August 14, 2009 at 9:28 am

Add New Comment Cancel reply

Your email address will not be published.

Glenn Jahnke

Glenn Jahnke
San Francisco

Recent Posts

  • 7/13/2012 – Happy Friday
  • iOS Acceptance and Ruby Keywords
  • Standup 1/9/2012
Subscribe to Glenn's Feed

Author Topics

rails (3)
ios (1)
ruby (8)
testing (1)
javascript (4)
jenkins (2)
tdd (2)
scala (2)
rubymine (2)
mysql (1)
agile (5)
iteration planning meetings (1)
performance (1)
jasmin (2)
rspec (1)
data processing (1)
cruisecontrol (1)
rvm (1)
android (1)
mobile (1)
mongodb (1)
concurrent programming (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >