2013-11-15 64 views
0

我可能会也可能不会发现RSpec代码中的错误,它在使用accep_nested_attributes_for时不会根据需要发布嵌套属性。RSpec控制器测试嵌套强参数

这里是我的控制器测试:

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 

这里的文件控制器的强大PARAMS:

​​

当测试职位的控制器,因为它不承认0 attachments_attributes被忽略键。但这就是属性应该如何实现的原理。

当我把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正在做的事情不会让它通过强大的参数。也许它只是通过散列而不是浏览器的查询字符串?我会继续挖掘。

有没有其他人处理过这个?谢谢!

  • 红宝石2.0.0p0(2013年2月24日修订版39474)[x86_64的-darwin12.2.0]
  • 导轨(4.0.0)
  • rspec的芯(2.14.5)
  • rspec的-rails(2.14.0)

回答

3

好吧伙计......我的宝贝。

属性必须被字符串索引。因此,而不是:

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”。没有发现错误。我现在去坐在角落里。

+0

Upvoted for the catch。 – eggmatters

相关问题