Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created May 15, 2012 00:26
Show Gist options
  • Save dchelimsky/2698271 to your computer and use it in GitHub Desktop.
Save dchelimsky/2698271 to your computer and use it in GitHub Desktop.
More source examples for blog on explicit use of subject
describe Article do
def article; Article.new :title => "Lorem ipsum"; end
it "has a title" do
article.title.should eq("Lorem ipsum")
end
end
describe Article do
it "has a title" do
article = Article.new :title => "Lorem ipsum"
article.title.should eq("Lorem ipsum")
end
end
# before
shared_exmamples_for "an auditable object" do
it "creates an audit record on save!" do
lambda { subject.save! }.should change(Audit, :count).by(1)
end
end
describe Person do
it_behaves_like "an auditable object" do
subject { Person.new :name => "David" }
end
end
# after
shared_exmamples_for "an auditable object" do
it "creates an audit record on save!" do
lambda { auditable.save! }.should change(Audit, :count).by(1)
# ^ ^ ^ ^ ^
end
end
describe Person do
it_behaves_like "an auditable object" do
def auditable; Person.new :name => "David"; end
# ^ ^ ^ ^ ^
end
end
describe AmericanCitizen do
context "18 years of age" do
subject { Person.new(:birthdate => 18.years.ago) }
it { should be_able_to(:vote) }
it { should be_able_to(:enlist) }
it { should_not be_able_to(:drink) } # srsly?
end
end
describe CheckingAccount, "with a non-zero starting balance" do
subject(:account) { CheckingAccount.new(Money.new(50, :USD)) }
it { should_not be_overdrawn }
it "has a balance equal to the starting balance" do
account.balance.should eq(Money.new(50, :USD))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment