Hello everyone,
I am having a hard time testing that my has_many and belongs_to
relationships are mapped to the correct DB columns. Does anyone have any
suggestions or good strategies for testing that these relationships are
properly mapped throughout an application?
An example follows:
Let’s say I have two models: Person and PhoneNumber. The Person model
has two fields which should map to PhoneNumber, namely primary_number_id
and fax_number_id. In my models, I could have something like:
class Person < ActiveRecord::Base
belongs_to :primary_number, :class_name => ‘PhoneNumber’, :foreign_key
=> ‘primary_number_id’
belongs_to :fax_number, :class_name => ‘PhoneNumber’, :foreign_key =>
‘fax_number_id’
end
class PhoneNumber < ActiveRecord:Base
has_many :person_primary , :class_name => ‘Patient’, :foreign_key =>
‘primary_number_id’
has_many :person_fax , :class_name => ‘Patient’, :foreign_key =>
‘fax_number_id’
end
Using RSpec, I would be able to ensure that these relationships do
indeed exist, but I know no way of confirming that they are mapped to
the correct column, and have found no way of ensuring that some other
relationship isn’t mapping to the same DB column and possibly replacing
the values down the line (for example, if both belongs_to relationships
above were mapped to the same :class_name/:foreign_key combination… or
even some other relationship from another model, etc…) I guess what is
needed is some way of testing that relationships do not conflict with
each other.
I hope my example is clear enough. Thoughts?
Thanks in advance!
Luis