matchers Collection (tutti matchers che affermano contro stringhe, hash e array) sono stati astratti fuori in un gioiello individuale, rspec-collection_matchers .
Per poter utilizzare questi matchers aggiungere questo al vostro Gemfile
:
gem 'rspec-collection_matchers'
O il vostro .gemspec
se stai lavorando su una gemma:
spec.add_development_dependency 'rspec-collection_matchers'
Quindi, aggiungere questo al tuo spec_helper.rb
:
require 'rspec/collection_matchers'
E quindi sarete in grado di utilizzare matchers di raccolta nella vostra spec:
require spec_helper
describe 'array' do
subject { [1,2,3] }
it { is_expected.to have(3).items }
it { is_expected.to_not have(2).items }
it { is_expected.to_not have(4).items }
it { is_expected.to have_exactly(3).items }
it { is_expected.to_not have_exactly(2).items }
it { is_expected.to_not have_exactly(4).items }
it { is_expected.to have_at_least(2).items }
it { is_expected.to have_at_most(4).items }
# deliberate failures
it { is_expected.to_not have(3).items }
it { is_expected.to have(2).items }
it { is_expected.to have(4).items }
it { is_expected.to_not have_exactly(3).items }
it { is_expected.to have_exactly(2).items }
it { is_expected.to have_exactly(4).items }
it { is_expected.to have_at_least(4).items }
it { is_expected.to have_at_most(2).items }
end
noti che è possibile utilizzare in modo intercambiabile items
e characters
, sono solo la sintassi di zucchero, e il have
Matcher, e le sue varianti, può essere utilizzato su array, hash e la stringa .