I have done a lot of generic test writing in the last months, and one thing that always resurfaced was:
item = send(@obj.class.to_s.underscore.pluralize,:one) item = send(@item_name.to_s.pluralize.:one) ...
What does it do ? It load the fixture :one for this objects class, since i always keep a fixture :one and :two around to make generic tests less painful. But all this send madness was unreadable, so i factored out some fixture calling.
#spec/spec_helper.rb OR test/test_helper.rb
#helper for generic fixture calling
#fixture 'user','one' = users('one')
#fixture @user,'one' = users('one')
#fixture User,'one' = users('one')
def fixture(singular,name)
klas = input_to_class(singular)
fixture = find_fixture(klas,name)
raise "fixtures for #{klas} not loaded!" unless fixture
fixture
end
def find_fixture(klas,name)
table = klas.to_s.underscore.pluralize
return send(table,name) if respond_to? table
false
end
def input_to_class(val)
return val if val.kind_of? Class
return val.class if val.kind_of? ActiveRecord::Base
return val.to_s.classify.constantize
end