ArticlaとComment
というmodelがあり、コメントが30件以上は投稿できないというテストをRspecで実装したい場合に、
FactoryGirlなどで、30以上のCommentを作るのは無駄が多いため、Stubなどを用いて実装できないかとかんがえております。
リレーションのイメージは次の通りです。
article = Article.first article.comments.count
Stubを使う場合次のような実装を考えたのですが、これでは、エラーがでてしまいます。
# 30個以上生成したいため、仮に40にしました。 allow_any_instance_of(Comment).to receive(:count).and_return(40) これに対してのエラー Failure/Error: allow_any_instance_of(Comment).to receive(:count).and_return(40) Comment does not implement #count
上手くStubで実装できる方法は御座いますでしょうか?
宜しくお願いします。
回答2件
あなたの回答
tips
プレビュー