Skip to content

Instantly share code, notes, and snippets.

@subdigital
Created May 15, 2011 02:22
Show Gist options
  • Save subdigital/972835 to your computer and use it in GitHub Desktop.
Save subdigital/972835 to your computer and use it in GitHub Desktop.
Better rspec structure with subject & let blocks
#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
# 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