Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 102 lines (82 sloc) 3.759 kb
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
Feature: have(n).items matcher

RSpec provides several matchers that make it easy to set expectations about the
size of a collection. There are three basic forms:

* collection.should have(x).items
* collection.should have\_at\_least(x).items
* collection.should have\_at\_most(x).items

In addition, #have_exactly is provided as an alias to #have.

These work on any collection-like object--the object just needs to respond to #size
or #length (or both). When the matcher is called directly on a collection object,
the #items call is pure syntactic sugar. You can use anything you want here. These
are equivalent:

* collection.should have(x).items
* collection.should have(x).things

You can also use this matcher on a non-collection object that returns a collection
from one of its methods. For example, Dir#entries returns an array, so you could
set an expectation using the following:

Dir.new("my/directory").should have(7).entries

Scenario: have(x).items on a collection
Given a file named "have_items_spec.rb" with:
"""ruby
describe [1, 2, 3] do
it { should have(3).items }
it { should_not have(2).items }
it { should_not have(4).items }

it { should have_exactly(3).items }
it { should_not have_exactly(2).items }
it { should_not have_exactly(4).items }

it { should have_at_least(2).items }
it { should have_at_most(4).items }

# deliberate failures
it { should_not have(3).items }
it { should have(2).items }
it { should have(4).items }

it { should_not have_exactly(3).items }
it { should have_exactly(2).items }
it { should have_exactly(4).items }

it { should have_at_least(4).items }
it { should have_at_most(2).items }
end
"""
When I run `rspec have_items_spec.rb`
Then the output should contain "16 examples, 8 failures"
And the output should contain "expected target not to have 3 items, got 3"
And the output should contain "expected 2 items, got 3"
And the output should contain "expected 4 items, got 3"
And the output should contain "expected at least 4 items, got 3"
And the output should contain "expected at most 2 items, got 3"

Scenario: have(x).words on a String when String#words is defined
Given a file named "have_words_spec.rb" with:
"""ruby
class String
def words
split(' ')
end
end

describe "a sentence with some words" do
it { should have(5).words }
it { should_not have(4).words }
it { should_not have(6).words }

it { should have_exactly(5).words }
it { should_not have_exactly(4).words }
it { should_not have_exactly(6).words }

it { should have_at_least(4).words }
it { should have_at_most(6).words }

# deliberate failures
it { should_not have(5).words }
it { should have(4).words }
it { should have(6).words }

it { should_not have_exactly(5).words }
it { should have_exactly(4).words }
it { should have_exactly(6).words }

it { should have_at_least(6).words }
it { should have_at_most(4).words }
end
"""
When I run `rspec have_words_spec.rb`
Then the output should contain "16 examples, 8 failures"
And the output should contain "expected target not to have 5 words, got 5"
And the output should contain "expected 4 words, got 5"
And the output should contain "expected 6 words, got 5"
And the output should contain "expected at least 6 words, got 5"
And the output should contain "expected at most 4 words, got 5"
Something went wrong with that request. Please try again.