I'm trying to test my controller in rSpec and get the following error:
1) ClientsController POST create with valid params creates a new Client
←[31mFailure/Error:←[0m ←[31mpost :create, {:client => valid_attributes}←[0
m
←[31mActiveModel::ForbiddenAttributesError←[0m:
←[31mActiveModel::ForbiddenAttributesError←[0m
←[36m # ./app/controllers/clients_controller.rb:27:in `create'←[0m
I assume it has something to do with the strong parameters. But I have no idea how to set the strong parameters in my rspec test controller (Because it's all describes instead of methods( def
))
I am using let(:valid_attributes) { FactoryGirl.build(:client).attributes }
however.
Is the forbiddenattributeserror something from the strong parameters? If so; How do I set my strong parameters in my controller. If not; What does cause this problem?
Here's the Factory I'm using with my :valid_attributes
:
FactoryGirl.define do
factory :client do
name { 'Willem' }
birthdate { '1990-10-10' }
background { '#ff0000'}
end
end
One of the failures is:
1) ClientsController PUT update with valid params updates the requested client
←[31mFailure/Error:←[0m ←[31mput :update, {:id => client.to_param, :client
=> valid_attributes}←[0m
←[31m#<Client:0x6c8e140> received :update with unexpected arguments←[0m
←[31m expected: ({"id"=>nil, "name"=>"Willem", "avatar"=>nil, "birthdate
"=>Wed, 10 Oct 1990, "background"=>"#ff0000", "group_id"=>nil, "created_at"=>nil
, "updated_at"=>nil})←[0m
←[31m got: ({"name"=>"Willem", "avatar"=>nil, "birthdate"=>"1990-10
-10", "background"=>"#ff0000", "group_id"=>nil})←[0m
←[36m # ./app/controllers/clients_controller.rb:44:in `block in update'←[0m
←[36m # ./app/controllers/clients_controller.rb:43:in `update'←[0m
←[36m # ./spec/controllers/clients_controller_spec.rb:112:in `block (4 level
s) in <top (required)>'←[0m
The description leads to:
it "updates the requested client" do
client = Client.create! valid_attributes
Client.any_instance.should_receive(:update).with(valid_attributes)
put :update, {:id => client.to_param, :client => valid_attributes}
end
You don't have to set the strong parameters in RSpec, just be sure to send the right object, example:
post new_client_path, client: FactoryGirl.build(:client).attributes
Else, what is the server log saying? There should be an error of what is causing this problem. Could you provide a copy of it here?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.