加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用应用程序来测试多态协议?

发布时间:2020-12-16 20:15:51 所属栏目:百科 来源:网络整理
导读:我正在使用带有rails的应用程序,我正在创建一个名为“comments”的模型,另一个名为“post”的模型.评论是多态的. 当我用这样的帖子测试与肩膀匹配者 it {should have_many(:comments)} 它得到这个消息 Expected Post to have a has_many association called
我正在使用带有rails的应用程序,我正在创建一个名为“comments”的模型,另一个名为“post”的模型.评论是多态的.

当我用这样的帖子测试与肩膀匹配者

it {should have_many(:comments)}

它得到这个消息

Expected Post to have a has_many
association called comments (Comment
does not have a post_id foreign key.)

在我的评论模型中

belongs_to :commentable,:polymorphic => true

如何测试我的多态关联,以便一个帖子可以有很多评论?

附: shoulda matcher文档表示它支持多态关联.

解决方法

你应该不需要做任何特别的测试,如果它应该工作.在您的帖子模型上,确保设置:as:
has_many :comments,:as => :commentable

这将确保rails使用适当的列名称commentable_id和commentable_type而不是post_id.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读