Oct 29, 2008

How to use fixtures with merb

I am using RSpec as the test framework:

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




222