RSpec控制器测试嵌套的强参数

时间:2013-11-15 21:49:08

标签: ruby-on-rails rspec

我可能会或者可能没有在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

当测试发布到控制器时,attachments_attributes会被忽略,因为它无法识别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仍然希望将attachmentments_attributes编入索引,但RSpec正在做的事情不会让它通过强参数。也许它只是直接传递哈希而不是浏览器的查询字符串?我会继续挖掘。

之前还有其他人处理过此事吗?谢谢!

  • ruby​​ 2.0.0p0(2013-02-24修订版39474)[x86_64-darwin12.2.0]
  • rails(4.0.0)
  • rspec-core(2.14.5)
  • rspec-rails(2.14.0)

1 个答案:

答案 0 :(得分:4)

好的伙计......我的baaad。

属性必须是字符串索引。所以而不是:

  attachments_attributes: {
    0 => {
      attachment: fixture_file_upload('files/document.json', 'application/json')
    }
  }

我必须这样做:

  attachments_attributes: {
    "0" => {
      attachment: fixture_file_upload('files/document.json', 'application/json')
    }
  }

所以是的。 0应该是“0”。这里没有发现错误。我现在就坐在角落里。