Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Forked from richievos/1-before_spec.rb
Created November 21, 2011 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjeanes/1382828 to your computer and use it in GitHub Desktop.
Save bjeanes/1382828 to your computer and use it in GitHub Desktop.
Spectastrophe #1
# This assumes `is_appropriate?` is renamed to `appropriate?` which is more idiomatic anyway
describe FashionSituation do
describe "#is_appropriate?" do
context "when wearing only shorts" do
let(:wearing_only_shorts) { FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => location) }
subject { wearing_only_shorts }
context "at a restaurant" do
let(:location) { "restaurant" }
it { should_not be_appropriate }
end
context "at work" do
let(:location) { "work" }
it { should_not be_appropriate }
end
context "at the beach" do
let(:location) { "beach" }
it { should be_appropriate }
end
end
end
end
describe FashionSituation do
it "should be inappropriate at a restaurant" do
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "restaurant")
situation.should_not be_appropriate
end
it "should be inappropriate at work" do
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "work")
situation.should_not be_appropriate
end
it "should be appropriate at the beach" do
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach")
situation.should be_appropriate
end
end
#### OR making this slightly more realistic, and talking about valid versus invalid
# that actually is a case where I think having shared setup between tests is appropriate, because your tests really are really only valuable as a whole. a single one doesn't really prove anything
describe FashionSituation do
before { @situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach") }
it "should be invalid at a restaurant" do
@situation.location = 'restaurant'
situation.should_not be_valid
end
it "should be invalid at work" do
@situation.location = 'work'
situation.should_not be_valid
end
it "should be valid when setup properly" do
@situation.should be_valid
end
end
# Though, generally with these you could just have
describe FashionSituation do
it "should be invalid at a restaurant" do
Factory.build(:fashion_situation, :location => 'restaurant').should_not be_valid
end
it "should be invalid at work" do
Factory.build(:fashion_situation, :location => 'work').should_not be_valid
end
it "should be valid when setup properly" do
Factory.build(:fashion_situation, :location => 'beach').should be_valid
end
end
# not 100% sure which of these 2 I prefer, but I think the one with the before is more obvious and involves less hoops
describe FashionSituation do
describe "#is_appropriate?" do
context "at a restaurant" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "restaurant")
end
it "should be inappropriate" do
@situation.should_not be_appropriate
end
end
context "at work" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "work")
end
it "should be inappropriate" do
@situation.should_not be_appropriate
end
end
context "at the beach" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach")
end
it "should be appropriate" do
@situation.should be_appropriate
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment