Created
May 15, 2013 16:58
-
-
Save trinitronx/5585480 to your computer and use it in GitHub Desktop.
Common RSpec matcher cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just a nice quick example reference of some common RSpec matchers | |
# References: | |
# https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers | |
# http://rspec.rubyforge.org/rspec/1.1.9/classes/Spec/Matchers.html | |
describe "a simple RSpec matcher" do | |
it "should do Object Identity comparison" do | |
3.should be(3) | |
this = Array.new([1,2,3]) | |
this.should be this | |
this.should_not be [1,2,3] | |
end | |
it "should do Equality comparisons" do | |
true.should be_true | |
false.should be_false | |
nil.should be_nil | |
'this'.should_not be_nil | |
[].should be_empty | |
this = Array.new([1,2,3]) | |
this.should eq [1,2,3] | |
end | |
it "should do String matching comparisons" do | |
actual='3' | |
expected = '3' | |
actual.should =~ /3/ | |
actual.should match(/3/) | |
end | |
it "should handle Array & Range inclusion" do | |
[1].should include(1) | |
(1..10).should cover(3) | |
end | |
it "should handle exceptions & throws" do | |
expect { raise TypeError }.to raise_error | |
expect { throw :done }.to throw_symbol | |
end | |
it "should " do | |
{ :a => 1 }.should have_key(:a) | |
end | |
it "should do > comparisons" do | |
actual = 3 | |
expected = 2 | |
actual.should be > expected | |
actual.should be >= expected | |
end | |
it "should do < comparisons & delta comparison" do | |
actual = 3 | |
expected = 4 | |
delta = 1 | |
actual.should be <= expected | |
actual.should be < expected | |
actual.should be_within(delta).of(expected) | |
end | |
it "should do Equality comparisons" do | |
actual = 3 | |
expected = 3 | |
actual.should == expected # passes if actual == expected | |
actual.should eql(expected) # passes if actual.eql?(expected) | |
actual.should equal(expected) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment