Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2011 17:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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)
# Now we can write tests against like this:
it "should have the right author" do == author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment