LoginSignup
0
1

More than 5 years have passed since last update.

rspecのassignsで割り当てられた要素の個数をカウントする方法

Last updated at Posted at 2019-01-16

コントローラーspecのassignsでの割り当てテストの際に、どのように割り当てた要素数をテストするか実装の際に調べたので備忘録としてメモしておく。

方法

実際使用したコード

#related_productsに割り当てられている要素数は4個であるはず。
expect(assigns(:related_products).size).to eq 4

例のようにsizeを使用し、beeqで数字を指定すればOKぽい。

ちなみに以前は、

expect(assigns(:related_products)).to have(4).items

でいけたらしいがrspec3.0~は削除された模様。今後もし要素の個数を数えるテストが必要になった場合は上の方法を使用するのがよさそう。でも調べたらあまり該当記事がなかったのであまり使用するケースも多くないのかな〜

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1