Collection matchers (all matchers that assert against strings, hashes and arrays) have been abstracted out into a seperate gem, rspec-collection_matchers.
In order to use these matchers add this to your Gemfile
:
gem 'rspec-collection_matchers'
Or your .gemspec
if you're working on a gem:
spec.add_development_dependency 'rspec-collection_matchers'
Then, add this to your spec_helper.rb
:
require 'rspec/collection_matchers'
And then you'll be able to use collection matchers in your 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
Note that you can use items
and characters
interchangeably, they're just syntax-sugar, and the have
matcher, and its variants, can be used on arrays, hashes and your string.