Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created May 14, 2012 03:03
Show Gist options
  • Save dchelimsky/2691548 to your computer and use it in GitHub Desktop.
Save dchelimsky/2691548 to your computer and use it in GitHub Desktop.
Source examples for blog on explicit use of subject
describe Article do
it { should validate_presence_of(:title) }
end
describe Article do
it "validates presence of :title" do
article = Article.new
article.should validate_presence_of(:title)
end
end
describe Article do
subject { Article.new }
# ...
# ...
it "validates presence of :title" do
subject.should validate_presence_of(:title)
end
end
# not recommended
describe Article do
subject { Article.new }
it "validates presence of :title" do
subject.should validate_presence_of(:title)
end
end
describe Article do
def article; Article.new; end
it "validates presence of :title" do
article.should validate_presence_of(:title)
end
end
# refactoring toward implicit step 1
describe Article do
it "validates presence of :title" do
subject.should validate_presence_of(:title)
end
end
# refactoring toward implicit step 2
describe Article do
it "validates presence of :title" do
should validate_presence_of(:title)
end
end
describe Article do
subject { Article.new :title => "Lorem ipsum" }
it "has a title" do
subject.title.should eq("Lorem ipsum")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment