Created
November 5, 2011 02:04
-
-
Save bbasata/1340982 to your computer and use it in GitHub Desktop.
A simple example of RSpec
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
class Integer | |
def factorial | |
raise if self < 0 | |
self > 0 ? (1..self).reduce(&:*) : 1 | |
end | |
end | |
describe Integer, '#factorial' do | |
context "for values larger than 1" do | |
example { 2.factorial.should == 2*1 } | |
example { 3.factorial.should == 3*2*1 } | |
example { 4.factorial.should == 4*3*2*1 } | |
example { 8.factorial.should == 8*7*6*5*4*3*2*1 } | |
end | |
context "for 1" do | |
example { 1.factorial.should == 1 } | |
end | |
context "for 0" do | |
example { 0.factorial.should == 1 } | |
end | |
context "for negative numbers" do | |
example { expect { -1.factorial }.to raise_error } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment