module RSpecial::Matchers
This module provides matchers for RSpec-compatiblity.
The set is not fully compataible, but provides most RSpec matchers. The most notable exlusion for the moment is the ‘have` matchers.
Compatability will improve with time. Feel obligated to submit a patch if you really need it. ;)
@see www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
Public Instance Methods
Passes if expected and actual are nto equal within delta tolerance.
value.should be_close(delta, criterion)
@raise WithinAssay
# File lib/rspecial/matchers.rb, line 42 def be_close(delta, criterion) WithinAssay.assertor(criterion, delta) end
Passes if object is empty.
object.should be_empty
@raise EmptyAssay
# File lib/rspecial/matchers.rb, line 52 def be_empty EmptyAssay.assertor end
Passed if object is false
.
value.should be_false
@raise FalseAssay
# File lib/rspecial/matchers.rb, line 88 def be_false FalseAssay.assertor end
Passes if object is an instance of class.
object.should be_instance_of(class)
@raise InstanceAssay
# File lib/rspecial/matchers.rb, line 123 def be_instance_of(cls) InstanceAssay.assertor(cls) end
Pass if object is a kind of class.
object.should be_kind_of(class)
@raise KindAssay
# File lib/rspecial/matchers.rb, line 136 def be_kind_of(cls) KindAssay.assertor(cls) end
Passes if the expected and actual are alike.
object.should be_like(criterion)
There is no equivalant for this in RSpec, we simply add it here to cover all Assays available.
@raise LikeAssay
# File lib/rspecial/matchers.rb, line 32 def be_like(criterion) LikeAssay.assertor(criterion) end
Pass if object is nil
.
value.should be_nil
@raise NilAssay
# File lib/rspecial/matchers.rb, line 162 def be_nil NilAssay.assertor end
Passed if object is true
.
object.should be_true
@raise TrueAssay
# File lib/rspecial/matchers.rb, line 223 def be_true TrueAssay.assertor end
Passes if ‘actual == expected`.
object.should eq(object)
@raise EqualAssay
# File lib/rspecial/matchers.rb, line 112 def eq(obj) EqualAssay.assertor(obj) end
Pass if two objects are equal using ‘#eql?` method.
object1.should eql(object2)
@raise EqualityAssay
# File lib/rspecial/matchers.rb, line 198 def eql(exp) EqualityAssay.assertor(exp) end
Passes if actual is the same exact object as expected.
object.should be_identical_to(object)
@raise IdentityAssay
# File lib/rspecial/matchers.rb, line 99 def equal(obj) IdentityAssay.assertor(obj) end
Passes if expected
== actual
.
'MY STRING'.should equate_to('my string'.upcase) 'MY STRING'.should_not equate_to('another string')
This matcher is not supported by RSpec, but is added so that the EqualityAssay has an explict matcher available.
@raise EqualityAssay
# File lib/rspecial/matchers.rb, line 66 def equate_to(exp) EqualityAssay.assertor(exp) end
# File lib/rspecial/matchers.rb, line 252 def expect(target) Expect.new(target) end
# File lib/rspecial/matchers.rb, line 230 def have(n) Have.new(n) end
# File lib/rspecial/matchers.rb, line 239 def have_at_least(n) Have.new(n, :at_least) end
# File lib/rspecial/matchers.rb, line 246 def have_at_most(n) Have.new(n, :at_most) end
Pass if object matches pattern using ‘#=~` method.
object.should match(regexp)
@raise MatchAssay
# File lib/rspecial/matchers.rb, line 151 def match(regexp) MatchAssay.assertor(regexp) end
Pass if an exception is raised.
lambda { do_something_risky }.should raise_error lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
@raise RaiseAssay
@todo Support for message matching ?
# File lib/rspecial/matchers.rb, line 176 def raise_error(exception=Exception) RaiseAssay.assertor(exception) end
Pass if object responds to message.
object.should respond_to(:method_name)
@raise RespondAssay
# File lib/rspecial/matchers.rb, line 187 def respond_to(method) RespondAssay.assertor(method) end
Passes if block is satisfied given target object as argument.
5.should satisfy{ |n| n > 3}
@raise ExecutionAssay
# File lib/rspecial/matchers.rb, line 77 def satisfy(&block) ExecutionAssay.assertor(&block) end
Pass if procedure throws a specified symbol.
lambda { do_something_risky }.should throw_symbol lambda { do_something_risky }.should throw_symbol(:that_was_risky)
@raise ThrowAssay
@todo Support for throw argument. (Does RSpec even support this?)
# File lib/rspecial/matchers.rb, line 212 def throw_symbol(sym=nil) #, arg=nil) ThrowAssay.assertor(sym) end