Created
October 8, 2010 03:55
-
-
Save masarakki/616331 to your computer and use it in GitHub Desktop.
auto mock_xxxx hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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