Created
March 6, 2011 17:22
-
-
Save chrisyour/857424 to your computer and use it in GitHub Desktop.
Super handy way to modify and return the instance of an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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