File: README.md

package info (click to toggle)
ruby-rspec-collection-matchers 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 176 kB
  • ctags: 77
  • sloc: ruby: 919; sh: 7; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,916 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# RSpec::CollectionMatchers [![Build Status](https://secure.travis-ci.org/rspec/rspec-collection_matchers.png?branch=master)](http://travis-ci.org/rspec/rspec-collection_matchers)

RSpec::CollectionMatchers lets you express expected outcomes on collections
of an object in an example.

    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
end

it '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
```

## 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)