RSpec::Matchers

Public Class Methods

clear_generated_description() click to toggle source
# File lib/rspec/matchers/generated_descriptions.rb, line 7
def self.clear_generated_description
  self.last_matcher = nil
  self.last_should = nil
end
generated_description() click to toggle source
# File lib/rspec/matchers/generated_descriptions.rb, line 12
def self.generated_description
  return nil if last_should.nil?
  "#{last_should.to_s.gsub('_',' ')} #{last_description}"
end

Public Instance Methods

should be_true click to toggle source
should be_false
should be_nil
should be_[arbitrary_predicate](*args)
should_not be_nil
should_not be_[arbitrary_predicate](*args)

Given true, false, or nil, will pass if actual value is true, false or nil (respectively). Given no args means the caller should satisfy an if condition (to be or not to be).

Predicates are any Ruby method that ends in a "?" and returns true or false. Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match convert that into a query against the target object.

The arbitrary_predicate feature will handle any predicate prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.

Examples

target.should be_true
target.should be_false
target.should be_nil
target.should_not be_nil

collection.should be_empty #passes if target.empty?
target.should_not be_empty #passes unless target.empty?
target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
# File lib/rspec/matchers/be.rb, line 210
def be(*args)
  args.empty? ?
    Matchers::Be.new : equal(*args)
end
be_a(klass) click to toggle source

passes if target.kind_of?(klass)

# File lib/rspec/matchers/be.rb, line 216
def be_a(klass)
  be_a_kind_of(klass)
end
Also aliased as: be_an
should be_kind_of(expected) click to toggle source
should be_a_kind_of(expected)
should_not be_kind_of(expected)
should_not be_a_kind_of(expected)

Passes if actual.kind_of?(expected)

Examples

5.should be_kind_of(Fixnum)
5.should be_kind_of(Numeric)
5.should_not be_kind_of(Float)
# File lib/rspec/matchers/be_kind_of.rb, line 16
def be_a_kind_of(expected)
  Matcher.new :be_a_kind_of, expected do |_expected_|
    match do |actual|
      actual.kind_of?(_expected_)
    end
  end
end
Also aliased as: be_kind_of
be_an(klass) click to toggle source
Alias for: be_a
should be_instance_of(expected) click to toggle source
should be_an_instance_of(expected)
should_not be_instance_of(expected)
should_not be_an_instance_of(expected)

Passes if actual.instance_of?(expected)

Examples

5.should be_instance_of(Fixnum)
5.should_not be_instance_of(Numeric)
5.should_not be_instance_of(Float)
# File lib/rspec/matchers/be_instance_of.rb, line 16
def be_an_instance_of(expected)
  Matcher.new :be_an_instance_of, expected do |_expected_|
    match do |actual|
      actual.instance_of?(_expected_)
    end
  end
end
Also aliased as: be_instance_of
should be_close(expected, delta) click to toggle source
should_not be_close(expected, delta)

Passes if actual == expected +/- delta

Example

result.should be_close(3.0, 0.5)
# File lib/rspec/matchers/be_close.rb, line 12
def be_close(expected, delta)
  Matcher.new :be_close, expected, delta do |_expected_, _delta_|
    match do |actual|
      (actual - _expected_).abs < _delta_
    end

    failure_message_for_should do |actual|
      "expected #{_expected_} +/- (< #{_delta_}), got #{actual}"
    end

    failure_message_for_should_not do |actual|
      "expected #{_expected_} +/- (< #{_delta_}), got #{actual}"
    end

    description do
      "be close to #{_expected_} (within +- #{_delta_})"
    end
  end
end
be_instance_of(expected) click to toggle source
Alias for: be_an_instance_of
be_kind_of(expected) click to toggle source
Alias for: be_a_kind_of
should change(receiver, message, &block) click to toggle source
should change(receiver, message, &block).by(value)
should change(receiver, message, &block).from(old).to(new)
should_not change(receiver, message, &block)

Allows you to specify that a Proc will cause some value to change.

Examples

lambda {
  team.add_player(player) 
}.should change(roster, :count)

lambda {
  team.add_player(player) 
}.should change(roster, :count).by(1)

lambda {
  team.add_player(player) 
}.should change(roster, :count).by_at_least(1)

lambda {
  team.add_player(player)
}.should change(roster, :count).by_at_most(1)    

string = "string"
lambda {
  string.reverse!
}.should change { string }.from("string").to("gnirts")

lambda {
  person.happy_birthday
}.should change(person, :birthday).from(32).to(33)

lambda {
  employee.develop_great_new_social_networking_app
}.should change(employee, :title).from("Mail Clerk").to("CEO")

Evaluates receiver.message or block before and after it evaluates the c object (generated by the lambdas in the examples above).

Then compares the values before and after the receiver.message and evaluates the difference compared to the expected difference.

WARNING

should_not change only supports the form with no subsequent calls to by, by_at_least, by_at_most, to or from.

blocks passed to should change and should_not change must use the {} form (do/end is not supported).

# File lib/rspec/matchers/change.rb, line 180
def change(receiver=nil, message=nil, &block)
  Matchers::Change.new(receiver, message, &block)
end
should eq(expected) click to toggle source
should_not eq(expected)

Passes if actual == expected.

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

5.should eq(5)
5.should_not eq(3)
# File lib/rspec/matchers/eq.rb, line 15
def eq(expected)
  Matcher.new :eq, expected do |_expected_|

    diffable

    match do |actual|
      actual == _expected_
    end

    failure_message_for_should do |actual|
      expected #{_expected_.inspect}     got #{actual.inspect}(compared using ==)
    end

    failure_message_for_should_not do |actual|
      expected #{actual.inspect} not to equal #{_expected_.inspect}(compared using ==)
    end

    description do
      "== #{_expected_}"
    end
  end
end
should eql(expected) click to toggle source
should_not eql(expected)

Passes if actual and expected are of equal value, but not necessarily the same object.

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

5.should eql(5)
5.should_not eql(3)
# File lib/rspec/matchers/eql.rb, line 15
def eql(expected)
  Matcher.new :eql, expected do |_expected_|

    diffable

    match do |actual|
      actual.eql?(_expected_)
    end

    failure_message_for_should do |actual|
      expected #{_expected_.inspect}     got #{actual.inspect}(compared using eql?)
    end

    failure_message_for_should_not do |actual|
      expected #{actual.inspect} not to equal #{_expected_.inspect}(compared using eql?)
    end
  end
end
should equal(expected) click to toggle source
should_not equal(expected)

Passes if actual and expected are the same object (object identity).

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

5.should equal(5) #Fixnums are equal
"5".should_not equal("5") #Strings that look the same are not the same object
# File lib/rspec/matchers/equal.rb, line 16
def equal(expected)
  Matcher.new :equal, expected do |_expected_|
    match do |actual|
      actual.equal?(_expected_)
    end
    
    def inspect_object(o)
      "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
    end
    
    failure_message_for_should do |actual|
      expected #{inspect_object(_expected_)}     got #{inspect_object(actual)}Compared using equal?, which compares object identity,but expected and actual are not the same object. Use'actual.should == expected' if you don't care aboutobject identity in this example.
    end

    failure_message_for_should_not do |actual|
      expected not #{inspect_object(actual)}         got #{inspect_object(_expected_)}Compared using equal?, which compares object identity.
    end
  end
end
should exist click to toggle source
should_not exist

Passes if actual.exist?

# File lib/rspec/matchers/exist.rb, line 8
def exist(arg=nil)
  Matcher.new :exist do
    match do |actual|
      arg ? actual.exist?(arg) : actual.exist?
    end
  end
end
should have(number).named_collection__or__sugar click to toggle source
should_not have(number).named_collection__or__sugar

Passes if receiver is a collection with the submitted number of items OR if the receiver OWNS a collection with the submitted number of items.

If the receiver OWNS the collection, you must use the name of the collection. So if a Team instance has a collection named #players, you must use that name to set the expectation.

If the receiver IS the collection, you can use any name you like for named_collection. We'd recommend using either "elements", "members", or "items" as these are all standard ways of describing the things IN a collection.

This also works for Strings, letting you set an expectation about its length

Examples

# Passes if team.players.size == 11
team.should have(11).players

# Passes if [1,2,3].length == 3
[1,2,3].should have(3).items #"items" is pure sugar

# Passes if "this string".length == 11
"this string".should have(11).characters #"characters" is pure sugar
# File lib/rspec/matchers/have.rb, line 122
def have(n)
  Matchers::Have.new(n)
end
Also aliased as: have_exactly
should have_at_least(number).items click to toggle source

Exactly like have() with >=.

Warning

should_not have_at_least is not supported

# File lib/rspec/matchers/have.rb, line 135
def have_at_least(n)
  Matchers::Have.new(n, :at_least)
end
should have_at_most(number).items click to toggle source

Exactly like have() with <=.

Warning

should_not have_at_most is not supported

# File lib/rspec/matchers/have.rb, line 147
def have_at_most(n)
  Matchers::Have.new(n, :at_most)
end
have_exactly(n) click to toggle source
Alias for: have
should include(expected) click to toggle source
should_not include(expected)

Passes if actual includes expected. This works for collections and Strings. You can also pass in multiple args and it will only pass if all args are found in collection.

Examples

[1,2,3].should include(3)
[1,2,3].should include(2,3) #would pass
[1,2,3].should include(2,3,4) #would fail
[1,2,3].should_not include(4)
"spread".should include("read")
"spread".should_not include("red")
# File lib/rspec/matchers/include.rb, line 19
def include(*expected)
  Matcher.new :include, *expected do |*_expected|
    match_for_should do |actual|
      perform_match(:all?, :all?, actual, _expected)
    end

    match_for_should_not do |actual|
      perform_match(:none?, :any?, actual, _expected)
    end

    def perform_match(predicate, hash_predicate, actual, _expected)
      _expected.send(predicate) do |expected|
        if comparing_hash_values?(actual, expected)
          expected.send(hash_predicate) {|k,v| actual[k] == v}
        elsif comparing_hash_keys?(actual, expected)
          actual.has_key?(expected)
        else
          actual.include?(expected)
        end
      end
    end

    def comparing_hash_keys?(actual, expected) # :nodoc:
      actual.is_a?(Hash) && !expected.is_a?(Hash)
    end

    def comparing_hash_values?(actual, expected) # :nodoc:
      actual.is_a?(Hash) && expected.is_a?(Hash)
    end
  end
end
inspect_object(o) click to toggle source
# File lib/rspec/matchers/equal.rb, line 22
def inspect_object(o)
  "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
end
should match(pattern) click to toggle source
should_not match(pattern)

Given a Regexp or String, passes if actual.match(pattern)

Examples

email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
email.should match("@example.com")
# File lib/rspec/matchers/match.rb, line 13
def match(expected)
  Matcher.new :match, expected do |_expected_|
    match do |actual|
      actual.match(_expected_)
    end
  end
end
perform_match(predicate, hash_predicate, actual, _expected) click to toggle source
# File lib/rspec/matchers/include.rb, line 29
def perform_match(predicate, hash_predicate, actual, _expected)
  _expected.send(predicate) do |expected|
    if comparing_hash_values?(actual, expected)
      expected.send(hash_predicate) {|k,v| actual[k] == v}
    elsif comparing_hash_keys?(actual, expected)
      actual.has_key?(expected)
    else
      actual.include?(expected)
    end
  end
end
should raise_error() click to toggle source
should raise_error(NamedError)
should raise_error(NamedError, String)
should raise_error(NamedError, Regexp)
should raise_error() { |error| ... }
should raise_error(NamedError) { |error| ... }
should raise_error(NamedError, String) { |error| ... }
should raise_error(NamedError, Regexp) { |error| ... }
should_not raise_error()
should_not raise_error(NamedError)
should_not raise_error(NamedError, String)
should_not raise_error(NamedError, Regexp)

With no args, matches if any error is raised. With a named error, matches only if that specific error is raised. With a named error and messsage specified as a String, matches only if both match. With a named error and messsage specified as a Regexp, matches only if both match. Pass an optional block to perform extra verifications on the exception matched

Examples

lambda { do_something_risky }.should raise_error
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)

lambda { do_something_risky }.should_not raise_error
lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
# File lib/rspec/matchers/raise_error.rb, line 125
def raise_error(error=Exception, message=nil, &block)
  Matchers::RaiseError.new(error, message, &block)
end
Also aliased as: raise_exception
raise_exception(error=Exception, message=nil, &block) click to toggle source
Alias for: raise_error
should respond_to(*names) click to toggle source
should_not respond_to(*names)

Matches if the target object responds to all of the names provided. Names can be Strings or Symbols.

Examples

# File lib/rspec/matchers/respond_to.rb, line 81
def respond_to(*names)
  Matchers::RespondTo.new(*names)
end
should satisfy {} click to toggle source
should_not satisfy {}

Passes if the submitted block returns true. Yields target to the block.

Generally speaking, this should be thought of as a last resort when you can't find any other way to specify the behaviour you wish to specify.

If you do find yourself in such a situation, you could always write a custom matcher, which would likely make your specs more expressive.

Examples

5.should satisfy { |n|
  n > 3
}
# File lib/rspec/matchers/satisfy.rb, line 47
def satisfy(&block)
  Matchers::Satisfy.new(&block)
end
should throw_symbol() click to toggle source
should throw_symbol(:sym)
should throw_symbol(:sym, arg)
should_not throw_symbol()
should_not throw_symbol(:sym)
should_not throw_symbol(:sym, arg)

Given no argument, matches if a proc throws any Symbol.

Given a Symbol, matches if the given proc throws the specified Symbol.

Given a Symbol and an arg, matches if the given proc throws the specified Symbol with the specified arg.

Examples

lambda { do_something_risky }.should throw_symbol
lambda { do_something_risky }.should throw_symbol(:that_was_risky)
lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)

lambda { do_something_risky }.should_not throw_symbol
lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)
# File lib/rspec/matchers/throw_symbol.rb, line 100
def throw_symbol(expected_symbol = nil, expected_arg=nil)
  Matchers::ThrowSymbol.new(expected_symbol, expected_arg)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.