Created
October 14, 2012 20:49
-
-
Save esmevane/3889782 to your computer and use it in GitHub Desktop.
[Ruby / Rspec] A tiny rspec example
This file contains 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
# Actual filename: .rspec | |
--format doc | |
--color |
This file contains 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
# Actual filename: roll_spec.rb | |
require './roll' | |
describe Roll do | |
context "with default sides" do | |
let(:roll) { Roll.new } | |
subject { roll } | |
its(:result) { should_not > 6 } | |
its(:result) { should_not < 1 } | |
its(:sides) { should == 6 } | |
end | |
context "with custom sides" do | |
let(:sides) { 8 } | |
let(:roll) { Roll.new sides } | |
subject { roll } | |
its(:result) { should_not > sides } | |
its(:result) { should_not < 1 } | |
its(:sides) { should == sides } | |
end | |
context "with bonuses" do | |
let(:bonus) { 4 } | |
let(:roll) { Roll.new } | |
let(:thrown_dice) { roll.throw_with_bonus bonus } | |
let(:high) { roll.sides + bonus } | |
let(:low) { 1 + bonus } | |
subject { roll } | |
its(:sides) { should == 6 } | |
describe '#throw_with_bonus' do | |
it "adds bonuses to roll results" do | |
result = (thrown_dice >= low && thrown_dice <= high) | |
result.should be_true | |
end | |
end | |
end | |
end |
This file contains 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
# Actual filename: roll.rb | |
class Roll | |
attr_accessor :sides | |
def initialize sides = 6 | |
@sides ||= sides | |
end | |
def result | |
rand(@sides) + 1 | |
end | |
def throw_with_bonus bonus = 0 | |
result + bonus | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment