Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dchelimsky/973165 to your computer and use it in GitHub Desktop.
Save dchelimsky/973165 to your computer and use it in GitHub Desktop.
Better rspec structure with subject & let blocks
describe Card do
describe "#value" do
context "Two of Hearts" do
subject { Card.new("2H") }
its(:value) { should == 2 }
end
describe "Face Cards" do
context "King of Clubs" do
subject { Card.new("KC") }
its(:value) { should == 13 }
end
context "Queen of Clubs" do
subject{ Card.new("QC") }
its(:value) { should == 12 }
end
context "Jack of Hearts" do
subject{ Card.new("JH") }
its(:value) { should == 11 }
end
end
context "Bad Value" do
it "should raise StandardError" do
expect { Card.new("ZZ") }.to raise_error(StandardError)
end
end
end
end
Card
::new
given a bad value
raises
#value
is the same as the number
is 1 for an Ace
is 11 for a Jack
is 12 for a Queen
is 13 for a King
require "rspec/expectations"
require "wrong/adapters/rspec"
RSpec.configuration.expect_with :stdlib
Wrong.config.alias_assert :expect
describe Card do
describe "::new" do
context "given a bad value" do
it "raises" do
assert { rescuing { Card.new("ZZ") } }
end
end
end
describe "#value" do
it "is the same as the number" do
(2..10).to_a.each do |n|
assert { Card.new("#{n}H").value == n }
end
end
it "is 1 for an Ace" do
assert { Card.new("AH").value == 1 }
end
it "is 11 for a Jack" do
assert { Card.new("JH").value == 11 }
end
it "is 12 for a Queen" do
assert { Card.new("QH").value == 12 }
end
it "is 13 for a King" do
assert { Card.new("KH").value == 13 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment