2013-11-15 68 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

相關問題