Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec gives ActiveModel::MassAssignmentSecurity::Error

I'm following Railstutorial.org and gets MassAssignment Error when using Rspec.

10) User when email format is invalid should be invalid
     Failure/Error: @user = User.new(name:"Example", email:"[email protected]",
     ActiveModel::MassAssignmentSecurity::Error:
       Can't mass-assign protected attributes: password, password_confirmation

Probably because I try to assign before variables in RSpec:

  ...
  before do
     @user = User.new(name:"Example", email:"[email protected]", 
                                password: "foobar", password_confirmation: "foobar" )
  end

  subject { @user }
  ...

Is it possible to disable MassAssignment protection in development or test mode? Or when RSpec is running? Any help would be great! Thanks

like image 242
YogiZoli Avatar asked Dec 27 '22 03:12

YogiZoli


1 Answers

You could just avoid the mass assignment:

before do
  @user = User.new(name:"Example", email:"[email protected]").tap do |u|
    u.password = "foobar"
    u.password_confirmation = "foobar"
  end
end
like image 124
zetetic Avatar answered Dec 30 '22 08:12

zetetic