配列の要素全てが該当する条件を記述するallマッチャ
scopeみたいにフィルタリングした要素について、フィルタリング対象の要素について
let(:included) { Item.new(price: 250) } let(:not_included) { Item.new(price: 300) } let(:array) { [included, not_included] } subject(array.filtering) it '250円以下の品物が絞り込まれる' do expect(subject).to contain_exactly(included) expect(subject).not_to contain_exactly(not_included) end
のように、フィルターの境界値の値を持つ要素をフィルタリング前のコレクションに含めておいて、その要素が含まれる/含まれない、といった形でspecを書いていくこともできますが、allマッチャを使うことで
let(:sample1) { Item.new(price: 250) } let(:sample2) { Item.new(price: 300) } let(:array) { [sample1, sample2] } subject(array.filtering) it '250円以下の品物が絞り込まれる' do expect(subject.map(&:price)).to all(be <= 250) expect(subject.count).to eq 1 end
元の要素ではなく、絞り込まれた結果に注目してspecを書くことができます。