Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Tools
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Testing mass assignment with RSpec-Shoulda

Mark Rushakoff
Monday, May 28, 2012

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.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. bryanl says:

    _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._

    I’m having a hard time digesting this. Shoulda matchers are basically the same thing as writing your implementation right in your test. Now, is this probably a good idea?

    May 28, 2012 at 8:07 pm

  2. Mark Rushakoff says:

    Hi Bryan,

    I think this helper shines most in the should *not* case. If I see some column `foo` that isn’t included in `attr_accessible`, it might not be immediately obvious for `foo` whether it was intentionally excluded. The test would clarify the intent.

    On the other hand, I think I can agree with you about the positive assertion test not being very valuable. If you have a field as `attr_accessible`, you probably have *some* test that depends on it being accessible.

    May 28, 2012 at 10:54 pm

Add New Comment Cancel reply

Your email address will not be published.

Mark Rushakoff

Mark Rushakoff
San Francisco

Recent Posts

  • git rebase vs. git merge: an agile perspective
  • An attitude shift as we approach production
  • Use fold to wrap long lines for an easier diff
Subscribe to Mark's Feed

Author Topics

agile (1)
git (2)
merge (1)
rebase (1)
attitude (1)
cloudfoundry (1)
incident-response (1)
production (1)
diff (1)
shell script (1)
text-wrapping (1)
unix (1)
gem (1)
ruby (8)
ci (1)
jenkins (1)
rspec (2)
chrome (3)
two-step-authentication (1)
headphones (1)
pair programming (1)
software engineering (1)
javascript (5)
css (2)
backbone (2)
bash (1)
html (2)
documentation (2)
rails (6)
tdd (1)
carrierwave (1)
imagemagick (1)
compass (1)
github (1)
firefox (3)
jasmine (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Tools
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >