【发布时间】:2013-11-15 21:49:08
【问题描述】:
我可能会也可能不会在 RSpec 代码中发现一个错误,即在使用 Accepts_nested_attributes_for 时,它不允许它根据需要发布嵌套属性。
这是我的控制器测试:
it 'attaches a file to document' do
post :create, {
app_id: @app1.id,
document: {
recipient_id: @app2.id,
delivery_service: 'default',
attachments_attributes: {
0 => {
attachment: fixture_file_upload('files/document.json', 'application/json')
}
}
},
format: 'json'
}
attachment = assigns(:document).attachments.first
attachment.exists?.should be_true
attachment.url.should match 'amazon'
end
这是文档控制器的强参数:
private
def document_params
params.require(:document).permit(
:recipient_id,
:delivery_service,
:document_id,
:document_type,
attachments_attributes:
[:attachment, :attachment_file_name, :attachment_file_size, :attachment_content_type, :attachment_updated_at]
)
end
当测试发布到控制器时,附件属性会被忽略,因为它无法识别 0 键。但这就是属性应该是这样的,并且它可以实时工作。
当我取出 0 键并将其留在测试中时:
attachments_attributes: {
attachment: fixture_file_upload('files/document.json', 'application/json')
}
我收到undefined method '[]' for Tempfile
这是从我的控制器开始的回溯:
# ~/.rvm/gems/ruby-2.0.0-p0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb:40:in `method_missing'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/nested_attributes.rb:452:in `block in assign_nested_attributes_for_collection_association'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/nested_attributes.rb:452:in `map'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/nested_attributes.rb:452:in `assign_nested_attributes_for_collection_association'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/nested_attributes.rb:339:in `attachments_attributes='
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:42:in `public_send'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:53:in `block in assign_nested_parameter_attributes'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:53:in `each'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:53:in `assign_nested_parameter_attributes'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/core.rb:192:in `initialize'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/reflection.rb:189:in `build_association'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:242:in `build_record'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:114:in `build'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb:229:in `build'
# ./app/controllers/documents_controller.rb:17:in `create'
nested_attributes.rb 仍然希望 attachments_attributes 被索引,但是 RSpec 正在做的事情不会让它通过强参数。也许它只是直接传递哈希而不是像浏览器那样作为查询字符串?我会继续挖掘。
以前有没有其他人处理过这个问题?谢谢!
- ruby 2.0.0p0(2013-02-24 修订版 39474)[x86_64-darwin12.2.0]
- 导轨 (4.0.0)
- rspec-core (2.14.5)
- rspec-rails (2.14.0)
【问题讨论】:
标签: ruby-on-rails rspec