module Spec::Matchers
def be(*args)
target.should_not be_empty #passes unless target.empty?
"this string".should be_an_intance_of(String)
collection.should be_empty #passes if target.empty?
target.should_not be_nil
target.should be_nil
target.should be_false
target.should be_true
target.should be
== Examples
or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
The arbitrary_predicate feature will handle any predicate
convert that into a query against the target object.
Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
Predicates are any Ruby method that ends in a "?" and returns true or false.
the caller should satisfy an if condition (to be or not to be).
true, false or nil (respectively). Given no args means
Given true, false, or nil, will pass if actual is
should_not be_arbitrary_predicate(*args)
should_not be_nil
should be_arbitrary_predicate(*args)
should be_nil
should be_false
should be_true
should be
:call-seq:
def be(*args) Matchers::Be.new(*args) end
def be_close(expected, delta)
== Example
Passes if actual == expected +/- delta
should_not be_close(expected, delta)
should be_close(expected, delta)
:call-seq:
def be_close(expected, delta) Matchers::BeClose.new(expected, delta) end
def change(target=nil, message=nil, &block)
blocks passed to +should+ +change+ and +should_not+ +change+
+by+, +by_at_least+, +by_at_most+, +to+ or +from+.
+should_not+ +change+ only supports the form with no subsequent calls to
== Warning
evaluates the difference compared to the expected difference.
Then compares the values before and after the +receiver.message+ and
after it evaluates the c object (generated by the lambdas in the examples above).
Evaluates +receiver.message+ or +block+ before and
}.should change(employee, :title).from("Mail Clerk").to("CEO")
employee.develop_great_new_social_networking_app
lambda {
}.should change(person, :birthday).from(32).to(33)
person.happy_birthday
lambda {
}.should change { string }.from("string").to("gnirts")
string.reverse!
lambda {
string = "string"
}.should change(roster, :count).by_at_most(1)
team.add_player(player)
lambda {
}.should change(roster, :count).by_at_least(1)
team.add_player(player)
lambda {
}.should change(roster, :count).by(1)
team.add_player(player)
lambda {
}.should change(roster, :count)
team.add_player(player)
lambda {
== Examples
Allows you to specify that a Proc will cause some value to change.
should_not change(receiver, message, &block)
should change(receiver, message, &block).from(old).to(new)
should change(receiver, message, &block).by(value)
should change(receiver, message, &block)
:call-seq:
def change(target=nil, message=nil, &block) Matchers::Change.new(target, message, &block) end
def eql(expected)
5.should eql(5)
== Examples
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
Passes if actual and expected are of equal value, but not necessarily the same object.
should_not eql(expected)
should eql(expected)
:call-seq:
def eql(expected) Matchers::Eql.new(expected) end
def equal(expected)
5.should equal(5) #Fixnums are equal
== Examples
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
Passes if actual and expected are the same object (object identity).
should_not equal(expected)
should equal(expected)
:call-seq:
def equal(expected) Matchers::Equal.new(expected) end
def exist; Exist.new; end
def exist; Exist.new; end
def have(n)
# Passes if "this string".length == 11
[1,2,3].should have(3).items #"items" is pure sugar
# Passes if [1,2,3].length == 3
team.should have(11).players
# Passes if team.players.size == 11
== Examples
about its length
This also works for Strings, letting you set an expectation
standard ways of describing the things IN a collection.
either "elements", "members", or "items" as these are all
you like for named_collection. We'd recommend using
If the receiver IS the collection, you can use any name
to set the expectation.
collection named #players, you must use that name
of the collection. So if a Team instance has a
If the receiver OWNS the collection, you must use the name
with the submitted number of items.
number of items OR if the receiver OWNS a collection
Passes if receiver is a collection with the submitted
should_not have(number).named_collection__or__sugar
should have(number).named_collection__or__sugar
:call-seq:
def have(n) Matchers::Have.new(n) end
def have_at_least(n)
== Warning
Exactly like have() with >=.
should have_at_least(number).items
:call-seq:
def have_at_least(n) Matchers::Have.new(n, :at_least) end
def have_at_most(n)
== Warning
Exactly like have() with <=.
should have_at_most(number).items
:call-seq:
def have_at_most(n) Matchers::Have.new(n, :at_most) end
def include(*expected)
"spread".should include("read")
[1,2,3].should_not include(4)
[1,2,3].should include(2,3,4) #would fail
[1,2,3].should include(2,3) #would pass
[1,2,3].should include(3)
== Examples
and it will only pass if all args are found in collection.
collections and Strings. You can also pass in multiple args
Passes if actual includes expected. This works for
should_not include(expected)
should include(expected)
:call-seq:
def include(*expected) Matchers::Include.new(*expected) end
def match(regexp)
== Examples
Given a Regexp, passes if actual =~ regexp
should_not match(regexp)
should match(regexp)
:call-seq:
def match(regexp) Matchers::Match.new(regexp) end
def method_missing(sym, *args, &block) # :nodoc:
def method_missing(sym, *args, &block) # :nodoc: return Matchers::Be.new(sym, *args) if sym.starts_with?("be_") return Matchers::Has.new(sym, *args) if sym.starts_with?("have_") super end
def raise_error(error=Exception, message=nil, &block)
lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
lambda { do_something_risky }.should_not raise_error
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
lambda { do_something_risky }.should raise_error
== Examples
Pass an optional block to perform extra verifications on the exception matched
With a named error and messsage specified as a Regexp, matches only if both match.
With a named error and messsage specified as a String, matches only if both match.
With a named error, matches only if that specific error is raised.
With no args, matches if any error is raised.
should_not raise_error(NamedError, Regexp)
should_not raise_error(NamedError, String)
should_not raise_error(NamedError)
should_not raise_error()
should raise_error(NamedError, Regexp) { |error| ... }
should raise_error(NamedError, String) { |error| ... }
should raise_error(NamedError) { |error| ... }
should raise_error() { |error| ... }
should raise_error(NamedError, Regexp)
should raise_error(NamedError, String)
should raise_error(NamedError)
should raise_error()
:call-seq:
def raise_error(error=Exception, message=nil, &block) Matchers::RaiseError.new(error, message, &block) end
def respond_to(*names)
== Examples
provided. Names can be Strings or Symbols.
Matches if the target object responds to all of the names
should_not respond_to(*names)
should respond_to(*names)
:call-seq:
def respond_to(*names) Matchers::RespondTo.new(*names) end
def satisfy(&block)
n > 3
5.should satisfy { |n|
== Examples
a custom matcher, which would likely make your specs more expressive.
If you do find yourself in such a situation, you could always write
specify.
you can't find any other way to specify the behaviour you wish to
Generally speaking, this should be thought of as a last resort when
block.
Passes if the submitted block returns true. Yields target to the
should_not satisfy {}
should satisfy {}
:call-seq:
def satisfy(&block) Matchers::Satisfy.new(&block) end
def simple_matcher(message, &match_block)
def simple_matcher(message, &match_block) SimpleMatcher.new(message, &match_block) end
def throw_symbol(sym=nil)
lambda { do_something_risky }.should_not throw_symbol
lambda { do_something_risky }.should throw_symbol(:that_was_risky)
lambda { do_something_risky }.should throw_symbol
== Examples
Given no argument, matches if a proc throws any Symbol.
Given a Symbol argument, matches if a proc throws the specified Symbol.
should_not throw_symbol(:sym)
should_not throw_symbol()
should throw_symbol(:sym)
should throw_symbol()
:call-seq:
def throw_symbol(sym=nil) Matchers::ThrowSymbol.new(sym) end