I have a Ruby-Cucumber framework. I need to use the method method_missing
to avoid writing a few repetitive functions. Where should I place my method_missing
? In any .rb
file under the support
folder? Or should it go in some different specific file?
Define method_missing
like you would any Cucumber helper method, in an .rb
file in the support
directory and in a module which you then pass to the Cucumber World
:
module MethodMissing
def method_missing(method)
puts "You said #{method}"
end
end
World MethodMissing
You can then call missing methods in step definitions.
Like any Cucumber helper method, method_missing
should be defined only on the Cucumber World
. If you defined it at the top level of the support file, it would be defined on the Ruby top-level object and available everywhere, which is unparsimonious and might break other code.
I deliberately didn't defer to super
, since the World
doesn't define method_missing
, or define respond_to_missing?
, since I didn't have any plans to call method(:foo)
on the World
, but you can do those things if you prefer to always do them when you define method_missing
.