Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:39:59+00:00 2026-06-09T06:39:59+00:00

When I want to test if attribute is / is not accessible with RSpec

  • 0

When I want to test if attribute is / is not accessible with RSpec I’m doing it like this

class Foo
  attr_accesible :something_else
end

describe Foo do
  it('author should not be accessible')    {lambda{described_class.new(:author=>true)}.should raise_error ActiveModel::MassAssignmentSecurity::Error}
  it('something_else should be accessible'){lambda{described_class.new(:something_else=>true)}.should_not raise_error ActiveModel::MassAssignmentSecurity::Error}
end

is there better way doing that ?

…thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:40:00+00:00Added an answer on June 9, 2026 at 6:40 am

    This is the way attribute accessibility tests are done in the Rails Tutorial, which I think are pretty good. So, in your case, the test code could be modified slightly to read like:

    describe Foo do
      describe "accessible attributes" do
        it "should not allow access to author" do
          expect do
            Foo.new(author: true)
          end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
        end
    
        it "should allow access to something_else" do
          expect do
            Foo.new(something_else: true)
          end.to_not raise_error(ActiveModel::MassAssignmentSecurity::Error)
        end
      end
    end
    

    If this isn’t what you were looking for, can you give us a better idea of what kind of solution you’re after when you ask if there is a "better way"?

    Edit

    You may be interested in the Shoulda ActiveModel matchers, which would clean up the code to just one line per test. Something like:

    it { should_not allow_mass_assignment_of(:author) }
    it { should allow_mass_assignment_of(:something_else) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Attribute Filter like this one: $(’input[name="email"]‘); I want to create a function
I want to add a ID and CLASS attribute to my view template. this
For a unit test I want to use the Range attribute from NUnit to
I want to test the login() action in my UsersController.php <?php class UsersController extends
I want to test if a sentence contains anything else than white-space characters. This
I have this string: <own:egna attribute1=1 attribute2=2>test</own:egna> I want to catch all attributes with
How can I test to see if a class contains a particular attribute? In
I would like to initialize a class, then call an attribute which was set
I want to assign a class attribute via a string object - but how?
I have a class library I want to unit test using Microsofts unit test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.