0

我正在尝试在 rSpec 中测试我的控制器并收到以下错误:

  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

我认为这与强参数有关。但我不知道如何在我的 rspec 测试控制器中设置强参数(因为它都是描述而不是方法(def))

但是我正在使用let(:valid_attributes) { FactoryGirl.build(:client).attributes }

禁止属性错误是来自强参数吗?如果是这样; 如何在我的控制器中设置我的强参数。如果不; 是什么导致了这个问题?

这是我与我一起使用的工厂:valid_attributes

FactoryGirl.define do
  factory :client do
    name { 'Willem' }
    birthdate { '1990-10-10' }
    background { '#ff0000'}
  end
end

失败之一是:

  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

描述导致:

  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
4

1 回答 1

0

您不必在 RSpec 中设置强参数,只需确保发送正确的对象,例如:

发布 new_client_path,客户端:FactoryGirl.build(:client).attributes

否则,服务器日志在说什么?导致此问题的原因应该有错误。你能在这里提供一份副本吗?

于 2013-10-21T14:58:58.410 回答