Die*_*zar 1 rspec ruby-on-rails
我可能会或可能不会在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
Run Code Online (Sandbox Code Playgroud)
这是文档控制器的强项:
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
Run Code Online (Sandbox Code Playgroud)
当测试发布到控制器时,attachments_attributes将被忽略,因为它无法识别0键。但这就是属性应有的方式,并且可以实时工作。
当我拿出0键并将其留在测试中时:
attachments_attributes: {
attachment: fixture_file_upload('files/document.json', 'application/json')
}
Run Code Online (Sandbox Code Playgroud)
我懂了 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'
Run Code Online (Sandbox Code Playgroud)
nested_attributes.rb仍然期望对attachments_attributes进行索引,但是RSpec所做的某些事情不允许它通过强参数。也许它只是直接传递哈希值,而不是像浏览器那样将其作为查询字符串传递?我会继续挖掘。
以前有人处理过吗?谢谢!
好的...我的宝贝
必须对属性进行字符串索引。所以代替:
attachments_attributes: {
0 => {
attachment: fixture_file_upload('files/document.json', 'application/json')
}
}
Run Code Online (Sandbox Code Playgroud)
我必须这样做:
attachments_attributes: {
"0" => {
attachment: fixture_file_upload('files/document.json', 'application/json')
}
}
Run Code Online (Sandbox Code Playgroud)
是啊 0应该是“ 0”。在这里找不到错误。我现在坐在角落里。
| 归档时间: |
|
| 查看次数: |
790 次 |
| 最近记录: |