Testing mass assignment with RSpec-Shoulda

May 28, 2012 Mark Rushakoff

If you’re new to Rails, or if you’ve been using Rails 2 for a long time, you might not be aware that Shoulda offers an allow_mass_assignment_of matcher that works just like it sounds. Here’s the example from the source code:

it { should_not allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:first_name) }

Having explicit tests for whether fields should be mass-assignable is probably safer than letting developers arbitrarily add or remove fields from the attr_accessible declarations — at least when they break a test they’ll have to think twice about it.

About the Author

Biography

Previous
Tuesday Brown Bag: @ykatz
Tuesday Brown Bag: @ykatz

Events Tuesday Brown Bag: Yehuda Yehuda Katz will be giving a brown bag on Tuesday at 12:30.

Next
Birchbox is looking for experienced RoR Engineers
Birchbox is looking for experienced RoR Engineers

At Pivotal Labs, one of the services we provide our clients is helping them interview and hire. Pivotal Lab...

×

Subscribe to our Newsletter

!
Thank you!
Error - something went wrong!