Ruby on rails 如何使用shoulda matchers测试多态关联?
我将shoulda matchers与rails一起使用,并创建了一个名为“comments”的模型和另一个名为“post”的模型。注释是多态的 当我在像这样的帖子中与shoulda matchers进行测试时Ruby on rails 如何使用shoulda matchers测试多态关联?,ruby-on-rails,shoulda,polymorphism,Ruby On Rails,Shoulda,Polymorphism,我将shoulda matchers与rails一起使用,并创建了一个名为“comments”的模型和另一个名为“post”的模型。注释是多态的 当我在像这样的帖子中与shoulda matchers进行测试时 it {should have_many(:comments)} 它收到了这个信息 预期的职位有很多 协会称之为评论(Comment) 没有post_id外键。) 在我的评论模型中,我有 belongs_to :commentable, :polymorphic =>
it {should have_many(:comments)}
它收到了这个信息
预期的职位有很多
协会称之为评论(Comment)
没有post_id外键。)
在我的评论模型中,我有
belongs_to :commentable, :polymorphic => true
我如何测试我的多态关联,以便一篇文章可以有很多评论
p、 shoulda matcher的文档说它支持多态关联 您不需要在测试中做任何特殊的事情,因为
should
应该可以正常工作。在post模型上,确保将:设置为选项:
has_many :comments, :as => :commentable
这将确保rails使用正确的列名commentable\u id
和commentable\u type
,而不是post\u id
您在post模型上有什么?我意识到这是一个老问题,但从未发布过解决方案。我问了同样的问题(),解决方案是在测试数据库(doh!)中运行迁移。