1

Has anyone found a way to write an rspec example for an ActiveRecord polymorphic association?

I'm used to using Thoughtbot's shoulda matches, but I think polymorphic's are beyond it's scope?

Just for clarity my models would have a similar pattern to this:

class Person < ActiveRecord::Base
  attr_accessible :name

  has_one :address, as: :location_data_source

  accepts_nested_attributes_for :address
end

class Company < ActiveRecord::Base
  attr_accessible :name

  has_one :address, as: :location_data_source

  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  attr_accessible :street, :city

  belongs_to :location_data_source, polymorphic: true
end
Neil Billingham
  • 2,235
  • 4
  • 23
  • 34
  • Can you be more specific with your question? Put another way, what aspect of your application's behavior do you feel you cannot test because of the use of polymorphic associations? Can you give an example of an RSpec test you tried to create? – Peter Alfvin Sep 09 '13 at 15:17
  • Hi Peter, I just want to make sure that the associations behave in an appropriate manner (not that I want to test active record itself, I just want to make sure I've put the association in place rather than discover the model is incorrect further up the line e.g. at the view/capybara level). I usually use Thoughtbot's shoulda matches for this, they work great for most associations, such as has_one or belongs to; for example 'should belong_to(:address)' would work fine. – Neil Billingham Sep 09 '13 at 15:44
  • Have you run your migrations on your test database? See http://stackoverflow.com/questions/18405058/how-do-i-get-shoulda-to-recognise-my-polymorphic-association and http://stackoverflow.com/questions/5244363/how-to-use-shoulda-matchers-to-test-a-polymorphic-assoication – Peter Alfvin Sep 10 '13 at 20:35

0 Answers0