Created
May 15, 2011 02:22
-
-
Save subdigital/972835 to your computer and use it in GitHub Desktop.
Better rspec structure with subject & let blocks
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
#improved example | |
require 'spec_helper' | |
describe Card do | |
subject do | |
Card.new(card_type) | |
end | |
describe "#value" do | |
context "Two of Hearts" do | |
let(:card_type) { "2H" } | |
its(:value) { should == 2 } | |
end | |
describe "Face Cards" do | |
context "King of Clubs" do | |
let(:card_type) { "KC" } | |
its(:value) { should == 13 } | |
end | |
context "Queen of Clubs" do | |
let(:card_type) { "QC" } | |
its(:value) { should == 12 } | |
end | |
context "Jack of Hearts" do | |
let(:card_type) { "JH" } | |
its(:value) { should == 11 } | |
end | |
end | |
context "Bad Value" do | |
it "should raise StandardError" do | |
expect { card = Card.new("ZZ") }.to raise_error(StandardError) | |
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
# original example | |
require 'spec_helper' | |
describe Card do | |
describe "#new" do | |
describe "Two of Hearts" do | |
it "has a value of 2" do | |
card = Card.new("2H") | |
card.value.should equal(2) | |
end | |
end | |
describe "King of Clubs" do | |
it "has a value of 13" do | |
card = Card.new("KC") | |
card.value.should equal(13) | |
end | |
end | |
describe "Queen of Clubs" do | |
it "has a value of 12" do | |
card = Card.new("QC") | |
card.value.should equal(12) | |
end | |
end | |
describe "Jack of Clubs" do | |
it "has a value of 11" do | |
card = Card.new("JC") | |
card.value.should equal(11) | |
end | |
end | |
describe "Bad Value" do | |
it "should raise StandardError" do | |
expect { card = Card.new("ZZ") }.to raise_error(StandardError) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment