I'm not sure why you can't write a normal rspec against these objects--- generally rspec tests the objects themselves and how their public methods are implemented (and, when appropriate, the internals of the object implementation).
The decision to make the attachment polymorphic -- which incidentally looks like a fine choice in this case -- is an implementation decision.
In these cases, I would test
* That a User can have many (add/remove) addresses
* That a Company can have many (add/remove) addresses)
* That an address that belongs to a User can be correctly instantiated (and addressable is the user you are expecting)
* That an address that belongs to a Company can be correctly instantiated (and addressable is the company you are expecting)
(Some might argue that the things I listed above are over-kill testing, since all you're really doing is testing active record. I generally do write a few of these style of tests for sanity, but tend to not go overboard testing every feature of the association interface --- those features provided by AR itself --- in my unit tests)
Remember, your rspec will say something like
address.addressable.should eq(user)
The decision to implement as polymorphic is an implementation decision, so although in your case addressable is a belong_to polymorphic association, it could easily be implemented as a method on the object itself. Your unit test should be abstract from that, making your implementation de-coupled from your test expectation.
-Jason
On Sep 26, 2014, at 11:01 AM, Jan Yo <lists@ruby-forum.com> wrote:
> class Address < ActiveRecord::Base
> belongs_to :addressable, polymorphic: true
> end
>
> My Second Model: app/models/user.rb
>
> class User < ActiveRecord::Base
> has_many :addresses, as: :addressable
> end
>
> My third model: app/models/company.rb
>
> class Company < ActiveRecord::Base
> has_many :addresses, as: :addressable
> end
>
> For Address table, I have this migration:
>
> class CreateAddresses < ActiveRecord::Migration
> def change
> create_table :addresses do |t|
> t.string :description
> t.references :addressable, polymorphic: true
> t.timestamps
> end
> end
> end
>
> --
> 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5eb515f9f606ff97fca7967dc3b96e66%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5B777EC1-098F-4CE6-9E04-A08BB590C20A%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment