Skip to content

Instantly share code, notes, and snippets.

@zhhz
Created October 30, 2008 02:02
Show Gist options
  • Save zhhz/20878 to your computer and use it in GitHub Desktop.
Save zhhz/20878 to your computer and use it in GitHub Desktop.
# spec_helper.rb
# Helper method to adding the fixtures into the tests
def fixtures(*files)
files.each do |file|
klass = begin
Kernel::const_get(
Extlib::Inflection.classify(
Extlib::Inflection.singularize(file.to_s)))
rescue
nil
end
entries = YAML::load_file(File.dirname(__FILE__) + "/fixtures/#{file}.yml")
# do a migrate to create the table to clear the records
klass.auto_migrate!
created_objs = {}
entries.each do |name, entry|
created_objs[name] = klass.create(entry)
end
self.instance_variable_set("@#{file}", created_objs)
self.class.class_eval do
define_method(file) do |key|
self.instance_variable_get("@#{file}")[key.to_s]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment