Skip to content

Instantly share code, notes, and snippets.

@chrisyour
Created March 6, 2011 17:22
Show Gist options
  • Save chrisyour/857424 to your computer and use it in GitHub Desktop.
Save chrisyour/857424 to your computer and use it in GitHub Desktop.
Super handy way to modify and return the instance of an object
# Ruby 1.9
# Object.tap
# Here we use tap to set the post factory's author (since Factory Girl doesn't create singleton instances).
# Using tap passes a block where you can "tap" into the object.
# At the end of the block, the object is returned.
let(:author) { Factory(:author) }
let(:post) {
Factory(:post).tap do |p|
post.update_attribute(:author, author)
end
}
# Now we can write tests against post.author like this:
it "should have the right author" do
post.author.should == author
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment