Skip to content

Instantly share code, notes, and snippets.

@masarakki
Created October 8, 2010 03:55
Show Gist options
  • Save masarakki/616331 to your computer and use it in GitHub Desktop.
Save masarakki/616331 to your computer and use it in GitHub Desktop.
auto mock_xxxx hack
#
# write it in the bottom of spec/spec_helper.rb
#
def method_missing(name, *args)
if matched = name.to_s.match(/mock_(.*)/)
klass = matched[1].classify.constantize
var_name = "@#{name.to_s}"
self.class.send(:define_method, name) do |*stubs|
stubs = stubs.try(:first) || {}
instance_variable_get(var_name) ||
instance_variable_set(var_name, mock_model(klass, stubs))
end
send(name, args.first)
else
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment