Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rspec/rspec-collection_matchers
Collection cardinality matchers, extracted from rspec-expectations
https://github.com/rspec/rspec-collection_matchers
Last synced: 4 days ago
JSON representation
Collection cardinality matchers, extracted from rspec-expectations
- Host: GitHub
- URL: https://github.com/rspec/rspec-collection_matchers
- Owner: rspec
- License: mit
- Created: 2013-07-22T04:51:15.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2024-08-04T12:18:38.000Z (5 months ago)
- Last Synced: 2024-11-24T00:35:05.194Z (29 days ago)
- Language: Ruby
- Size: 104 KB
- Stars: 192
- Watchers: 15
- Forks: 25
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
README
# RSpec::CollectionMatchers [![Build Status](https://secure.travis-ci.org/rspec/rspec-collection_matchers.svg?branch=main)](http://travis-ci.org/rspec/rspec-collection_matchers)
RSpec::CollectionMatchers lets you express expected outcomes on collections
of an object in an example.```ruby
expect(account.shopping_cart).to have_exactly(3).items
```## Install
Add this line to your application's Gemfile:
gem 'rspec-collection_matchers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rspec-collection_matchers
## Basic usage
First of all, you need to require rspec-collection matchers. Add the following line to your `spec_helper.rb`:
```ruby
require 'rspec/collection_matchers'
```Using `rspec-collection_matchers` you can match the number of items in a
collection directly, e.g.:```ruby
it 'matches number of items in a collection' do
expect([1,2,3]).to have_at_least(3).items
end
```You can also match the number of items returned by a method on an object, e.g.:
```ruby
class Cart
def initialize(*products)
@products = products
end
attr_reader :products
endit 'matches number of items returned from a method' do
cart = Cart.new('product a', 'product b')
expect(cart).to have_at_most(2).products
end
```The last line of the example expresses an expected outcome:
if `cart.products.size <= 2` then the example passes, otherwise it fails with a message like:expected at most 2 products, got 3
## Available matchers
```ruby
expect(collection).to have(n).items
expect(collection).to have_exactly(n).items
expect(collection).to have_at_most(n).items
expect(collection).to have_at_least(n).items
expect(collection).to have(:no).items
```## See also
* [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
* [http://github.com/rspec/rspec-core](http://github.com/rspec/rspec-core)
* [http://github.com/rspec/rspec-expectations](http://github.com/rspec/rspec-expectations)