Module: Spec::Matchers

Extended by:
DSL
Included in:
RspecWorld, Example::ExampleMethods, Expectations::Helper::HandCodedMock, Matcher, SubstanceTester
Defined in:
lib/vendor/plugins/rspec/lib/spec/matchers.rb,
lib/vendor/plugins/rspec/spec/spec_helper.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/be.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/dsl.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/eql.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/has.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/have.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/equal.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/exist.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/match.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/change.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/errors.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/pretty.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/include.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/matcher.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/satisfy.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/be_close.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/dsl_spec.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/eql_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/be_kind_of.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/have_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/match_array.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/equal_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb,
lib/vendor/plugins/rspec/spec/autotest/autotest_matchers.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/matcher_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/be_instance_of.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/method_missing.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/simple_matcher.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/be_close_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/operator_matcher.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/wrap_expectation.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/be_kind_of_spec.rb,
lib/vendor/plugins/rspec-rails/lib/spec/rails/matchers/change.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/throw_symbol_spec.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/be_instance_of_spec.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/simple_matcher_spec.rb,
lib/vendor/plugins/rspec/spec/spec/matchers/matcher_methods_spec.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/generated_descriptions.rb,
lib/vendor/plugins/rspec/lib/spec/matchers/extensions/instance_exec.rb,
lib/vendor/plugins/rspec/spec/spec/expectations/wrap_expectation_spec.rb,
lib/vendor/plugins/rspec-rails/lib/spec/rails/extensions/spec/matchers/have.rb

Overview

:nodoc:

Defined Under Namespace

Modules: DSL, InstanceExec, Pretty Classes: AutotestMappingMatcher, Be, Change, Has, Have, MatchArray, Matcher, MatcherError, NegativeOperatorMatcher, OperatorMatcher, PositiveOperatorMatcher, RaiseError, RespondTo, Satisfy, SimpleMatcher, ThrowSymbol

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

create, define

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

:nodoc:



3
4
5
6
7
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/method_missing.rb', line 3

def method_missing(sym, *args, &block) # :nodoc:
  return Matchers::Be.new(sym, *args) if sym.to_s =~ /^be_/
  return Matchers::Has.new(sym, *args) if sym.to_s =~ /^have_/
  super
end

Class Attribute Details

.last_matcherObject

:nodoc:



4
5
6
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/generated_descriptions.rb', line 4

def last_matcher
  @last_matcher
end

.last_shouldObject

:nodoc:



4
5
6
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/generated_descriptions.rb', line 4

def last_should
  @last_should
end

Class Method Details

.clear_generated_descriptionObject



7
8
9
10
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/generated_descriptions.rb', line 7

def self.clear_generated_description
  self.last_matcher = nil
  self.last_should = nil
end

.generated_descriptionObject



12
13
14
15
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/generated_descriptions.rb', line 12

def self.generated_description
  return nil if last_should.nil?
  "#{last_should.to_s.gsub('_',' ')} #{last_description}"
end

Instance Method Details

#be(*args) ⇒ Object

:call-seq:

should be_true
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)


199
200
201
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 199

def be(*args)
  Matchers::Be.new(*args)
end

#be_a(klass) ⇒ Object Also known as: be_an

passes if target.kind_of?(klass)



204
205
206
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 204

def be_a(klass)
  be_a_kind_of(klass)
end

#be_a_kind_of(expected) ⇒ Object Also known as: be_kind_of

:call-seq:

should be_kind_of(expected)
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)


16
17
18
19
20
21
22
# File 'lib/vendor/plugins/rspec/lib/spec/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

#be_an_instance_of(expected) ⇒ Object Also known as: be_instance_of

:call-seq:

should be_instance_of(expected)
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)


16
17
18
19
20
21
22
# File 'lib/vendor/plugins/rspec/lib/spec/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

#be_close(expected, delta) ⇒ Object

:call-seq:

should be_close(expected, delta)
should_not be_close(expected, delta)

Passes if actual == expected +/- delta

Example

result.should be_close(3.0, 0.5)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vendor/plugins/rspec/lib/spec/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

#change(receiver = nil, message = nil, &block) ⇒ Object

:call-seq:

should change(receiver, message, &block)
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).



147
148
149
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/change.rb', line 147

def change(receiver=nil, message=nil, &block)
  Matchers::Change.new(receiver, message, &block)
end

#eql(expected) ⇒ Object

:call-seq:

should eql(expected)
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)


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
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/eql.rb', line 15

def eql(expected)
  Matcher.new :eql, expected do |_expected_|
    match do |actual|
      actual.eql?(_expected_)
    end

    failure_message_for_should do |actual|
      <<-MESSAGE

expected #{_expected_.inspect}
 got #{actual.inspect}

(compared using eql?)
MESSAGE
    end

    failure_message_for_should_not do |actual|
      <<-MESSAGE

expected #{actual.inspect} not to equal #{_expected_.inspect}

(compared using eql?)
MESSAGE
    end
  end
end

#equal(expected) ⇒ Object

:call-seq:

should equal(expected)
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


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
# File 'lib/vendor/plugins/rspec/lib/spec/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|
      <<-MESSAGE

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 about
object identity in this example.

MESSAGE
    end

    failure_message_for_should_not do |actual|
      <<-MESSAGE

expected not #{inspect_object(actual)}
     got #{inspect_object(_expected_)}

Compared using equal?, which compares object identity.

MESSAGE
    end
  end
end

#exception_from(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/vendor/plugins/rspec/spec/spec_helper.rb', line 31

def exception_from(&block)
  exception = nil
  begin
    yield
  rescue StandardError => e
    exception = e
  end
  exception
end

#exist(arg = nil) ⇒ Object

:call-seq:

should exist
should_not exist

Passes if actual.exist?



8
9
10
11
12
13
14
# File 'lib/vendor/plugins/rspec/lib/spec/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

#failObject



23
24
25
# File 'lib/vendor/plugins/rspec/spec/spec_helper.rb', line 23

def fail
  raise_error(Spec::Expectations::ExpectationNotMetError)
end

#fail_with(message) ⇒ Object



27
28
29
# File 'lib/vendor/plugins/rspec/spec/spec_helper.rb', line 27

def fail_with(message)
  raise_error(Spec::Expectations::ExpectationNotMetError, message)
end

#have(n) ⇒ Object Also known as: have_exactly

:call-seq:

should have(number).named_collection__or__sugar
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


122
123
124
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/have.rb', line 122

def have(n)
  Matchers::Have.new(n)
end

#have_at_least(n) ⇒ Object

:call-seq:

should have_at_least(number).items

Exactly like have() with >=.

Warning

should_not have_at_least is not supported



135
136
137
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/have.rb', line 135

def have_at_least(n)
  Matchers::Have.new(n, :at_least)
end

#have_at_most(n) ⇒ Object

:call-seq:

should have_at_most(number).items

Exactly like have() with <=.

Warning

should_not have_at_most is not supported



147
148
149
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/have.rb', line 147

def have_at_most(n)
  Matchers::Have.new(n, :at_most)
end

#include(*expected) ⇒ Object

:call-seq:

should include(expected)
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")


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/include.rb', line 19

def include(*expected)
  Matcher.new :include, *expected do |*_expected_|
    match do |actual|
      helper(actual, *_expected_)
    end
    
    def helper(actual, *_expected_)
      _expected_.each do |expected|
        if actual.is_a?(Hash)
          if expected.is_a?(Hash)
            expected.each_pair do |k,v|
              return false unless actual[k] == v
            end
          else
            return false unless actual.has_key?(expected)
          end
        else
          return false unless actual.include?(expected)
        end
      end
      true
    end
  end
end

#map_specs(specs) ⇒ Object



33
34
35
# File 'lib/vendor/plugins/rspec/spec/autotest/autotest_matchers.rb', line 33

def map_specs(specs)
  AutotestMappingMatcher.new(specs)
end

#match(expected) ⇒ Object

:call-seq:

should match(pattern)
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")


13
14
15
16
17
18
19
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/match.rb', line 13

def match(expected)
  Matcher.new :match, expected do |_expected_|
    match do |actual|
      actual.match(_expected_)
    end
  end
end

#raise_error(error = Exception, message = nil, &block) ⇒ Object

:call-seq:

should raise_error()
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/)


125
126
127
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb', line 125

def raise_error(error=Exception, message=nil, &block)
  Matchers::RaiseError.new(error, message, &block)
end

#respond_to(*names) ⇒ Object

:call-seq:

should respond_to(*names)
should_not respond_to(*names)

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

Examples



67
68
69
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb', line 67

def respond_to(*names)
  Matchers::RespondTo.new(*names)
end

#run_with(options) ⇒ Object



41
42
43
# File 'lib/vendor/plugins/rspec/spec/spec_helper.rb', line 41

def run_with(options)
  ::Spec::Runner::CommandLine.run(options)
end

#satisfy(&block) ⇒ Object

:call-seq:

should satisfy {}
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
}


43
44
45
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/satisfy.rb', line 43

def satisfy(&block)
  Matchers::Satisfy.new(&block)
end

#simple_matcher(description = nil, &match_block) ⇒ Object

simple_matcher makes it easy for you to create your own custom matchers in just a few lines of code when you don’t need all the power of a completely custom matcher object.

The description argument will appear as part of any failure message, and is also the source for auto-generated descriptions.

The match_block can have an arity of 1 or 2. The first block argument will be the given value. The second, if the block accepts it will be the matcher itself, giving you access to set custom failure messages in favor of the defaults.

The match_block should return a boolean: true indicates a match, which will pass if you use should and fail if you use should_not. false (or nil) indicates no match, which will do the reverse: fail if you use should and pass if you use should_not.

An error in the match_block will bubble up, resulting in a failure.

Example with default messages

def be_even
  simple_matcher("an even number") { |given| given % 2 == 0 }
end

describe 2 do
  it "should be even" do
    2.should be_even
  end
end

Given an odd number, this example would produce an error message stating: expected “an even number”, got 3.

Unfortunately, if you’re a fan of auto-generated descriptions, this will produce “should an even number.” Not the most desirable result. You can control that using custom messages:

Example with custom messages

def rhyme_with(expected)
  simple_matcher("rhyme with #{expected.inspect}") do |given, matcher|
    matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
    matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
    given.rhymes_with? expected
  end
end

# OR

def rhyme_with(expected)
  simple_matcher do |given, matcher|
    matcher.description = "rhyme with #{expected.inspect}"
    matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
    matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
    given.rhymes_with? expected
  end
end

describe "pecan" do
  it "should rhyme with 'be gone'" do
    nut = "pecan"
    nut.extend Rhymer
    nut.should rhyme_with("be gone")
  end
end

The resulting messages would be:

description:              rhyme with "be gone"
failure_message:          expected "pecan" to rhyme with "be gone"
negative failure_message: expected "pecan" not to rhyme with "be gone"

Wrapped Expectations

Because errors will bubble up, it is possible to wrap other expectations in a SimpleMatcher.

def be_even
  simple_matcher("an even number") { |given| (given % 2).should == 0 }
end

BE VERY CAREFUL when you do this. Only use wrapped expectations for matchers that will always be used in only the positive (should) or negative (should_not), but not both. The reason is that is you wrap a should and call the wrapper with should_not, the correct result (the should failing), will fail when you want it to pass.



129
130
131
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/simple_matcher.rb', line 129

def simple_matcher(description=nil, &match_block)
  SimpleMatcher.new(description, &match_block)
end

#throw_symbol(sym = nil) ⇒ Object

:call-seq:

should throw_symbol()
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)


100
101
102
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb', line 100

def throw_symbol(sym=nil)
  Matchers::ThrowSymbol.new(sym)
end

#with_ruby(version) ⇒ Object



45
46
47
# File 'lib/vendor/plugins/rspec/spec/spec_helper.rb', line 45

def with_ruby(version)
  yield if RUBY_VERSION =~ Regexp.compile("^#{version.to_s}")
end

#wrap_expectation(matcher, &block) ⇒ Object

wraps an expectation in a block that will return true if the expectation passes and false if it fails (without bubbling up the failure).

This is intended to be used in the context of a simple matcher, and is especially useful for wrapping multiple expectations or one or more assertions from test/unit extensions when running with test/unit.

Examples

def eat_cheese(cheese)
  simple_matcher do |mouse, matcher|
    matcher.failure_message = "expected #{mouse} to eat cheese"
    wrap_expectation do |matcher|
      assert_eats_cheese(mouse)
    end
  end
end

describe Mouse do
  it "eats cheese" do
    Mouse.new.should eat_cheese
  end
end

You might be wondering “why would I do this if I could just say” assert_eats_cheese?“, a fair question, indeed. You might prefer to replace the word assert with something more aligned with the rest of your code examples. You are using rspec, after all.

The other benefit you get is that you can use the negative version of the matcher:

describe Cat do
  it "does not eat cheese" do
    Cat.new.should_not eat_cheese
  end
end

So in the event there is no assert_does_not_eat_cheese available, you’re all set!



45
46
47
48
49
50
51
52
53
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/wrap_expectation.rb', line 45

def wrap_expectation(matcher, &block)
  begin
    block.call(matcher)
    return true
  rescue Exception => e
    matcher.failure_message = e.message
    return false
  end
end