use*_*339 16 polymorphism ruby-on-rails shoulda
我正在使用带有rails的shoulda-matcher,我正在创建一个名为"comments"的模型和另一个名为"post"的模型.评论是多态的.
当我在帖子中使用shoulda匹配器进行测试时,就像这样
it {should have_many(:comments)}
Run Code Online (Sandbox Code Playgroud)
它收到了这条消息
预期Post有一个名为comments的has_many关联(Comment没有post_id外键.)
在我的评论模型中,我有
belongs_to :commentable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
如何测试我的多态关联,以便帖子可以有很多注释?
ps the shoulda matcher文档说它支持多态关联.
你应该不需要在测试中做任何特殊的事情,因为should它应该正常工作.在您的帖子模型上,请确保您设置:as选项:
has_many :comments, :as => :commentable
Run Code Online (Sandbox Code Playgroud)
这将确保轨道使用适当的列名commentable_id和commentable_type,而不是post_id.
| 归档时间: |
|
| 查看次数: |
5200 次 |
| 最近记录: |