https://github.com/rspec/rspec-expectations
Provides a readable API to express expected outcomes of a code example
https://github.com/rspec/rspec-expectations
Keywords
rspec ruby
Keywords from Contributors
activerecord activejob mvc rubygems rubocop code-formatter static-code-analysis rack cucumber sinatra
Last synced: about 7 hours ago
JSON representation
Repository metadata
Provides a readable API to express expected outcomes of a code example
- Host: GitHub
- URL: https://github.com/rspec/rspec-expectations
- Owner: rspec
- License: mit
- Archived: true
- Created: 2009-06-29T15:58:17.000Z (over 16 years ago)
- Default Branch: main
- Last Pushed: 2024-11-30T12:37:23.000Z (over 1 year ago)
- Last Synced: 2026-02-24T17:19:40.499Z (7 days ago)
- Topics: rspec, ruby
- Language: Ruby
- Homepage: https://rspec.info
- Size: 5.44 MB
- Stars: 1,253
- Watchers: 37
- Forks: 385
- Open Issues: 0
- Releases: 10
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
README.md
RSpec Expectations

This is the old rspec expectations repository, please see the monorepo rspec/rspec for new issues and releases.
RSpec::Expectations lets you express expected outcomes on an object in an
example.
expect(account.balance).to eq(Money.new(37.42, :USD))
Install
If you want to use rspec-expectations with rspec, just install the rspec gem
and RubyGems will also install rspec-expectations for you (along with
rspec-core and rspec-mocks):
gem install rspec
Want to run against the main branch? You'll need to include the dependent
RSpec repos as well. Add the following to your Gemfile:
%w[rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'main'
end
If you want to use rspec-expectations with another tool, like Test::Unit,
Minitest, or Cucumber, you can install it directly:
gem install rspec-expectations
Contributing
Once you've set up the environment, you'll need to cd into the working
directory of whichever repo you want to work in. From there you can run the
specs and cucumber features, and make patches.
NOTE: You do not need to use rspec-dev to work on a specific RSpec repo. You
can treat each RSpec repo as an independent project.
Basic usage
Here's an example using rspec-core:
RSpec.describe Order do
it "sums the prices of the items in its line items" do
order = Order.new
order.add_entry(LineItem.new(:item => Item.new(
:price => Money.new(1.11, :USD)
)))
order.add_entry(LineItem.new(:item => Item.new(
:price => Money.new(2.22, :USD),
:quantity => 2
)))
expect(order.total).to eq(Money.new(5.55, :USD))
end
end
The describe and it methods come from rspec-core. The Order, LineItem, Item and Money classes would be from your code. The last line of the example
expresses an expected outcome. If order.total == Money.new(5.55, :USD), then
the example passes. If not, it fails with a message like:
expected: #<Money @value=5.55 @currency=:USD>
got: #<Money @value=1.11 @currency=:USD>
Built-in matchers
Equivalence
expect(actual).to eq(expected) # passes if actual == expected
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).not_to eql(not_expected) # passes if not(actual.eql?(expected))
Note: The new expect syntax no longer supports the == matcher.
Identity
expect(actual).to be(expected) # passes if actual.equal?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)
Comparisons
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_within(delta).of(expected)
Regular expressions
expect(actual).to match(/expression/)
Note: The new expect syntax no longer supports the =~ matcher.
Types/classes
expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected
expect(actual).to be_a(expected) # passes if actual.kind_of?(expected)
expect(actual).to be_an(expected) # an alias for be_a
expect(actual).to be_a_kind_of(expected) # another alias
Truthiness
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsy # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to_not be_nil # passes if actual is not nil
Expecting errors
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
Expecting throws
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
Yielding
expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args
expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded
expect { |b| 5.tap(&b) }.to yield_with_args(5)
expect { |b| 5.tap(&b) }.to yield_with_args(Integer)
expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
Predicate matchers
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
Ranges (Ruby >= 1.9 only)
expect(1..10).to cover(3)
Collection membership
# exact order, entire collection
expect(actual).to eq(expected)
# exact order, partial collection (based on an exact position)
expect(actual).to start_with(expected)
expect(actual).to end_with(expected)
# any order, entire collection
expect(actual).to match_array(expected)
# You can also express this by passing the expected elements
# as individual arguments
expect(actual).to contain_exactly(expected_element1, expected_element2)
# any order, partial collection
expect(actual).to include(expected)
Examples
expect([1, 2, 3]).to eq([1, 2, 3]) # Order dependent equality check
expect([1, 2, 3]).to include(1) # Exact ordering, partial collection matches
expect([1, 2, 3]).to include(2, 3) #
expect([1, 2, 3]).to start_with(1) # As above, but from the start of the collection
expect([1, 2, 3]).to start_with(1, 2) #
expect([1, 2, 3]).to end_with(3) # As above but from the end of the collection
expect([1, 2, 3]).to end_with(2, 3) #
expect({:a => 'b'}).to include(:a => 'b') # Matching within hashes
expect("this string").to include("is str") # Matching within strings
expect("this string").to start_with("this") #
expect("this string").to end_with("ring") #
expect([1, 2, 3]).to contain_exactly(2, 3, 1) # Order independent matches
expect([1, 2, 3]).to match_array([3, 2, 1]) #
# Order dependent compound matchers
expect(
[{:a => 'hash'},{:a => 'another'}]
).to match([a_hash_including(:a => 'hash'), a_hash_including(:a => 'another')])
should syntax
In addition to the expect syntax, rspec-expectations continues to support the
should syntax:
actual.should eq expected
actual.should be > 3
[1, 2, 3].should_not include 4
See detailed information on the should syntax and its usage.
Compound Matcher Expressions
You can also create compound matcher expressions using and or or:
expect(alphabet).to start_with("a").and end_with("z")
expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
Composing Matchers
Many of the built-in matchers are designed to take matchers as
arguments, to allow you to flexibly specify only the essential
aspects of an object or data structure. In addition, all of the
built-in matchers have one or more aliases that provide better
phrasing for when they are used as arguments to another matcher.
Examples
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(1.0) )
expect { s = "barn" }.to change { s }
.from( a_string_matching(/foo/) )
.to( a_string_matching(/bar/) )
expect(["barn", 2.45]).to contain_exactly(
a_value_within(0.1).of(2.5),
a_string_starting_with("bar")
)
expect(["barn", "food", 2.45]).to end_with(
a_string_matching("foo"),
a_value > 2
)
expect(["barn", 2.45]).to include( a_string_starting_with("bar") )
expect(:a => "food", :b => "good").to include(:a => a_string_matching(/foo/))
hash = {
:a => {
:b => ["foo", 5],
:c => { :d => 2.05 }
}
}
expect(hash).to match(
:a => {
:b => a_collection_containing_exactly(
a_string_starting_with("f"),
an_instance_of(Integer)
),
:c => { :d => (a_value < 3) }
}
)
expect { |probe|
[1, 2, 3].each(&probe)
}.to yield_successive_args( a_value < 2, 2, a_value > 2 )
Usage outside rspec-core
You always need to load rspec/expectations even if you only want to use one part of the library:
require 'rspec/expectations'
Then simply include RSpec::Matchers in any class:
class MyClass
include RSpec::Matchers
def do_something(arg)
expect(arg).to be > 0
# do other stuff
end
end
Also see
Owner metadata
- Name: RSpec
- Login: rspec
- Email:
- Kind: organization
- Description:
- Website: http://rspec.info
- Location:
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/22388?v=4
- Repositories: 18
- Last ynced at: 2024-03-25T19:34:00.441Z
- Profile URL: https://github.com/rspec
GitHub Events
Total
- Issues event: 15
- Watch event: 9
- Delete event: 2
- Issue comment event: 29
- Push event: 1
- Pull request event: 15
- Pull request review event: 1
- Fork event: 13
Last Year
- Watch event: 5
- Fork event: 2
Committers metadata
Last synced: 3 days ago
Total Commits: 2,208
Total Committers: 185
Avg Commits per committer: 11.935
Development Distribution Score (DDS): 0.685
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Myron Marston | m****n@g****m | 696 |
| David Chelimsky | d****y@g****m | 465 |
| Jon Rowe | h****o@j****k | 419 |
| Sam Phippen | s****n@g****m | 84 |
| Eric Mueller | n****a@g****m | 29 |
| lucapette | l****e@g****m | 25 |
| Yuji Nakayama | n****j@g****m | 24 |
| Benoit Tigeot | b****t@h****m | 22 |
| Phil Pirozhkov | h****o@f****u | 20 |
| Andy Lindeman | a****n@g****m | 18 |
| Pritesh Jain | p****6@g****m | 13 |
| Justin Ko | j****0@g****m | 12 |
| Eloy Espinaco | e****p@g****m | 12 |
| Xavier Shay | x****r@r****t | 12 |
| Chad Humphries | c****d@s****m | 11 |
| ChrisArcand | c****s@c****m | 8 |
| Markus Reiter | me@r****s | 8 |
| Alex Coplan | l****2@g****m | 8 |
| Ben Orenstein | b****n@g****m | 8 |
| Daniel Fone | d****l@f****z | 8 |
| Erik Michaels-Ober | s****k@g****m | 7 |
| K Gautam Pai | g****i@g****m | 7 |
| Sergio Gil | s****z@g****m | 7 |
| sleepingkingstudios | m****n@s****m | 7 |
| Zshawn Syed | z****1@g****m | 7 |
| Aaron Kromer | a****r@g****m | 6 |
| Bradley Schaefer | b****r@g****m | 6 |
| Jeremy Wadsack | j****k@g****m | 6 |
| Luke Redpath | l****e@l****k | 6 |
| Marc-Andre Lafortune | g****b@m****a | 6 |
| and 155 more... | ||
Committer domains:
- customink.com: 1
- castwide.com: 1
- gabrielgilder.com: 1
- gaurishsharma.com: 1
- nertzy.com: 1
- degraaff.org: 1
- brigade.com: 1
- umich.edu: 1
- crispdesign.net: 1
- zozi.com: 1
- panix.com: 1
- cwi.com.br: 1
- grandrounds.com: 1
- zieglers.ca: 1
- o2.pl: 1
- kaizeninternet.com: 1
- squareup.com: 1
- brandonturner.net: 1
- fedux.org: 1
- brynary.com: 1
- schito.me: 1
- teksol.info: 1
- coaster.com: 1
- brionesandco.com: 1
- ryantm.com: 1
- projective.io: 1
- zhang.su: 1
- holmes.io: 1
- timjwade.com: 1
- ovod-everett.org: 1
- codon.com: 1
- dblock.org: 1
- ojab.ru: 1
- brianjohn.com: 1
- gmx.de: 1
- markschneider.com: 1
- jamesalmond.com: 1
- jpcutler.net: 1
- orien.io: 1
- pablobm.com: 1
- freelancing-gods.com: 1
- bamaru.de: 1
- wimdu.com: 1
- petrofeed.com: 1
- seaandco.com: 1
- cookpad.com: 1
- me.com: 1
- neopoly.de: 1
- heroku.com: 1
- joshsoftware.com: 1
- therye.org: 1
- rebased.pl: 1
- fun-box.ru: 1
- highgroove.com: 1
- ratiodata.de: 1
- majority.co: 1
- futurelearn.com: 1
- oracle.com: 1
- plataformatec.com.br: 1
- jaredbeck.com: 1
- matijs.net: 1
- alanfoster.me: 1
- karns.name: 1
- mossity.com: 1
- foxsoft.net: 1
- goclio.com: 1
- alyssa.is: 1
- drw.com: 1
- ebay.com: 1
- mail.ru: 1
- tomstuart.co.uk: 1
- morewood.be: 1
- jarednorman.ca: 1
- pedrogimenez.me: 1
- pedro.bz: 1
- peterhiggins.org: 1
- marc-andre.ca: 1
- lukeredpath.co.uk: 1
- sleepingkingstudios.com: 1
- gautampai.com: 1
- fone.net.nz: 1
- reitermark.us: 1
- chrisarcand.com: 1
- spicycode.com: 1
- rhnh.net: 1
- fili.pp.ru: 1
- hopsandfork.com: 1
- jonrowe.co.uk: 1
Issue and Pull Request metadata
Last synced: 6 months ago
Total issues: 78
Total pull requests: 125
Average time to close issues: almost 2 years
Average time to close pull requests: 5 months
Total issue authors: 64
Total pull request authors: 42
Average comments per issue: 6.01
Average comments per pull request: 1.92
Merged pull request: 83
Bot issues: 0
Bot pull requests: 1
Past year issues: 4
Past year pull requests: 6
Past year average time to close issues: 8 days
Past year average time to close pull requests: 12 days
Past year issue authors: 4
Past year pull request authors: 3
Past year average comments per issue: 6.75
Past year average comments per pull request: 1.67
Past year merged pull request: 5
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- myronmarston (5)
- pirj (4)
- petergoldstein (2)
- henrahmagix (2)
- yujinakayama (2)
- segiddins (2)
- maxlinc (2)
- elado (1)
- Darhazer (1)
- alan-pie (1)
- benoittgt (1)
- tony612 (1)
- maxmeyer (1)
- romanbsd (1)
- jarednorman (1)
Top Pull Request Authors
- JonRowe (69)
- pirj (13)
- nevinera (11)
- bclayman-sq (7)
- genehsu (4)
- petergoldstein (4)
- r7kamura (3)
- ydah (3)
- bjfish (2)
- benoittgt (2)
- lnestor (2)
- myronmarston (2)
- jdelStrother (2)
- TylerRick (2)
- dblock (2)
Top Issue Labels
- small (2)
- bug (2)
- rspec 4 (1)
- feature request (1)
Top Pull Request Labels
- rspec 4 (4)
Package metadata
- Total packages: 2
- Total downloads: unknown
- Total dependent packages: 0 (may contain duplicates)
- Total dependent repositories: 0 (may contain duplicates)
- Total versions: 74
proxy.golang.org: github.com/rspec/rspec-expectations
- Homepage:
- Documentation: https://pkg.go.dev/github.com/rspec/rspec-expectations#section-documentation
- Licenses: mit
- Latest release: v3.13.3+incompatible (published over 1 year ago)
- Last Synced: 2026-03-01T14:01:10.361Z (2 days ago)
- Versions: 72
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Forks count: 1.474%
- Stargazers count: 1.92%
- Average: 5.943%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
guix: ruby-rspec-expectations
RSpec expectations library
- Homepage: https://github.com/rspec/rspec-expectations
- Documentation: https://git.savannah.gnu.org/cgit/guix.git/tree/gnu/packages/ruby-check.scm#n993
- Licenses: expat
- Latest release: 3.13.3 (published 1 day ago)
- Last Synced: 2026-03-02T19:00:21.401Z (1 day ago)
- Versions: 2
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
Dependencies
- github-markup >= 0 development
- redcarpet >= 0 development
- simplecov >= 0 development
- childprocess < 1.0.0
- childprocess > 1.0.0
- coderay >= 0
- contracts ~> 0.15.0
- cucumber <= 1.3.22
- diff-lcs ~> 1.4, >= 1.4.3
- ffi < 1.11.0
- ffi < 1.9.19
- ffi < 1.10
- ffi < 1.15
- ffi > 1.9.24
- jruby-openssl >= 0
- jruby-openssl < 0.10.0
- json > 2.3.0
- json < 2.0.0
- minitest < 5.12.0
- rake < 12.0.0
- rake < 11.0.0
- rake > 12.3.2
- rubocop ~> 1.0, < 1.12
- thor < 1.0.0
- thor > 1.0.0
- yard ~> 0.9.24
- aruba ~> 0.14.10 development
- cucumber >= 1.3 development
- minitest ~> 5.2 development
- rake > 10.0.0 development
- diff-lcs >= 1.2.0, < 2.0
- actions/checkout v3 composite
- ruby/setup-ruby v1 composite
Score: -Infinity