Evgeny asked me about my reflect_on_association post to find out if a one liner could be made to check several association values, that is, not just trusting that the association details would be correct.
You can't do it in a one liner, but this is how to make it as simple and DRY as possible for testing on multiple models.
First off, Class.reflect_on_association returns something akin to a hash, but not a hash. It actually returns an object of type ActiveRecord::Reflection::AssociationReflection
So, to test this, we need to get the data into a usable form.
This is done through a helper method in spec_helper.rb (if you are using RSpec.
I used this:
module ActiveRecord module Reflection class AssociationReflection def to_hash { :macro => @macro, :options => @options, :class_name => @class_name || @name.to_s.singularize.camelize } end end end end
Then, in the spec you are testing, you can do:
it "should test reflection details" do
association_results = { :macro => :belongs_to, :options => {:counter_cache => :true}, :class_name => "Location" }
User.reflect_on_association(:location).to_hash.should == association_results end
You can then use this method in any spec you want.
So, there you go Evgeny... not quite a one liner, but a lot easier to read!
blogLater
Mikel |
¿ 22/7/2007 - Sweeet!!!
But, any way to make this even prettier with the use of
"with_options"? (since we are dealing with hashes anyway)