I recently started using rspec and factory_girl and I’m working on a basic control spec for my create action in my projects controller.
So I have this as a before filter:
before :each do
@project = FactoryGirl.create(:project)
@user = FactoryGirl.create(:user, first_name: "Jim", last_name: "Smith", username: "jsmith")
session[:user_id] = @user.id # this maintains the session for the user created in the previous linew
end
My project expects to have a user associated with it.
So in the create spec, I have this:
describe 'POST #create' do
attribute_merge = FactoryGirl.attributes_for(:project).merge(FactoryGirl.attributes_for(:user))
context "with valid attributes" do
it "creates a new project" do
expect{
post :create, project: attribute_merge
}.to change(Project,:count).by(1)
end
end
end
So what I’m trying to do is pass the project attributes hash along with the user attributes hash because a project requires at least one user in order to be created. Now, the error I get is:
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: first_name, last_name....
I should add that my create action works perfectly in development and I do have attr_accessible :first_name, :last_name, :username,... in my user.rb file
It’s failing because your passing the actual attributes for the user to the project, instead of just a reference to the user.
Try