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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Nov-17 03:56 UTC
Re: RSpec Test: has_many and belongs_to conflicts
Luis Romero wrote in post #962011:> 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?Rails is well tested, so don''t test it further. Instead, make sure you''ve called has_many and belongs_to correctly. The easiest way to do that is by using reflect_on_association. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you Marnen, this is exactly what I needed...Not sure why I hadn''t run across this method before.... Luis -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.