Rails and Rspec - Has One Through Polymorphic -
i trying test has_one :through relationship in rails using shoulda matchers.
my relationship structure looks (it's reverse polymorphic association - https://gist.github.com/runemadsen/1242485 )
class container < activerecord::base has_many :contents has_many :videos, through: :contents, source: :item, source_type: "video" end class content < activerecord::base belongs_to :container, dependent: :destroy belongs_to :item, polymorphic: true, dependent: :destroy end class video < activerecord::base has_one :content, as: :item has_one :container, through: :contents end
this shoulda matcher code in rspec:
# video_spec.rb { should have_one(:container).through(:contents) }
the error is:
undefined method `klass' nil:nilclass
can tell me why i'm receiving error , how test association?
assuming that's all code in spec file (i apologize if posted trimmed down version), need nest shoulda matcher under describe
block video
class:
describe video { should have_one(:container).through(:contents) } end
the error you're getting indicates subject under test nil
. nesting spec inside describe
block video
class, subject under test implicitly video
class.
take peak @ documentation in source code, has ton of examples in it.
Comments
Post a Comment