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

method_missing hazardous to your module?

Ken Mayer
Friday, February 22, 2013

We built an(other) object factory module for our current project and it looks a lot like all the others:

After a while, we noticed that the create methods were all exactly the same. Time for dynamic refactoring!

Unfortunately, this leads to rather bizarre error messages during testing:

Failures:

  1) MyHelper#filter returns an empty filter if there's nothing in the session
     Failure/Error: not_my_method
     NameError:
       undefined local variable or method `not_my_method' for ...
     # ./spec/support/object_creation_methods.rb:141:in `method_missing'
     # ./spec/helpers/my_helper_spec.rb:14:in

We became our own enemy; Since we are including our module in RSpec for our tests, any missing method passed through our dynamic response method, with unfortunate, confusing results. Sad Panda.

There’s a refactoring pattern, “Replace Dynamic Method Receptor with Dynamic Method Definition” which translates run time #method_missing into methods defined at load time. No more crazy stack traces!

Thank you! Jay Fields, et al.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. Matthew Parker
    Matthew Parker says:

    Why are you reinventing Factory Girl?

    March 1, 2013 at 10:29 pm

  2. Ken Mayer
    Ken Mayer says:

    The technique described here is not restricted to the module’s purpose, I’m just using it as an example.

    That said, I have a love/hate relationship with FactoryGirl; While the DSL is really, really, nice, I can almost always get the exact same functionality with just plain old ruby. There’s less “magic” than using the gem. I’ve gotten “stuck” in weird places (with an inability to trace back the failure) using the FactoryGirl DSL when I wished I just had plain old ruby to write the factory.

    March 1, 2013 at 11:51 pm

Add New Comment Cancel reply

Your email address will not be published.

Ken Mayer

Ken Mayer
San Francisco

Recent Posts

  • Sencha Touch BDD – Part 5 – Controller Testing
  • Sencha Touch BDD – Part 4 – PhantomJS
  • Sencha Touch BDD – Part 3 – Testing Views and Mocking Stores
Subscribe to Ken's Feed

Author Topics

bdd (5)
bloggerdome (5)
jasmine (6)
javascript (5)
mobile (6)
sencha (5)
meta programming (1)
refactoring (1)
ruby (1)
agile (18)
planning (1)
design (2)
heroku (3)
rails (8)
workflow (5)
cucumber (3)
testing (4)
performance (3)
devops (1)
jquery (2)
careers (1)
college job fairs (1)
hiring (1)
jobs (1)
recruitment (1)
bundler (3)
facebook (1)
json (1)
nginx (1)
movember (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 >