method_missing hazardous to your module?

February 22, 2013 Ken Mayer

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.

About the Author

Biography

More Content by Ken Mayer
Previous
The Xtreme Labs Guide to Quality Assurance
The Xtreme Labs Guide to Quality Assurance

Have you ever downloaded an app that is buggy and crash-prone? If I download an app that doesn’t work prope...

Next
Building identical gems
Building identical gems

We ran into a problem where we were running `gem build` on identical input files and the built gems had dif...

How do you measure digital transformation?

Take the Benchmark