2015-12-09 23 views
2

我正在測試一個控制器,並且當我提交表單數據時,它的一些內容以字符串形式進入控制器,而不是我期望的哈希。Rspec Rails控制器提交表單參數URI編碼而不是哈希

def send_request(params={}) 
    patch :update, params.merge(:id => @company.id) 
end 

describe 'should_receive' do 
    let(:test_params) do 
    { 
     "utf8"=>"✓", 
     "_method"=>"patch", 
     "button"=>"", 
     "company"=> 
     { "label_ids"=>[""], 
     "customer_users_attributes"=>{"0"=>{"id"=>"3", "is_primary"=>"true"}}, 
     "baselines_attributes"=>{"0"=>{"label_ids"=>[""], "benchmark_ids"=>[""], "id"=>"1"}}}, 
     "action"=>"update", 
     "controller"=>"admin/companies", 
    } 
    end 

    after(:each) do 
    send_request(test_params) 
    end 

    it { @company.should_receive(:update).with(test_params).and_return(true) } 

當測試運行的錯誤:

Failure/Error: patch :update, params.merge(:id => @company.id) 
NoMethodError: 
    undefined method `has_key?' for "baselines_attributes%5Bsite_url%5D=site_url":String 

的PARAMS剛剛在失敗之前:

{"utf8"=>"✓", 
"_method"=>"patch", 
"button"=>"", 
"company"=> 
    "baselines_attributes%5B0%5D%5Bbenchmark_ids%5D%5B%5D=&baselines_attributes%5B0%5D%5Bid%5D=1&baselines_attributes%5B0%5D%5Blabel_ids%5D%5B%5D=&customer_users_attributes%5B0%5D%5Bid%5D=3&customer_users_attributes%5B0%5D%5Bis_primary%5D=true&label_ids%5B%5D=", 
"action"=>"update", 
"controller"=>"admin/companies", 
"id"=>"1"} 

在同一位置時,形式是通過瀏覽器提交的PARAMS :

{"utf8"=>"✓", 
"_method"=>"patch", 
"button"=>"", 
"company"=> 
    {"label_ids"=>[""], 
    "customer_users_attributes"=>{"0"=>{"id"=>"3", 
    "is_primary"=>"true"}}, 
    "baselines_attributes"=>{ 
    "0"=>{"label_ids"=>[""], "benchmark_ids"=>[""], "id"=>"1"}}}, 
"action"=>"update", 
"controller"=>"admin/companies", 
"id"=>"1"} 

在測試中,params [:company]中的所有內容都是一個URI編碼的字符串,而在實際代碼中,它恰當地作爲散列來使用。

如何才能正確處理測試數據?

更新

的Ruby 2.2.0

相關寶石:

  • 軌(4.0.13)
  • rspec的核心(2.14.8)
  • nested_form( 0.3.2)
+0

你可以發佈你的標記嗎?你在使用'simple_form'嗎? – richessler

+0

我認爲發佈標記會讓人分心,但我會添加有關寶石/版本的詳細信息。代碼正常工作,這是測試是問題。 – agbodike

回答

0

爲什麼不使用FactoryGirl來構建屬性?

+0

我們在其他地方使用FactoryGirl,但我沒有寫測試。我的猜測是作者只是測試簡單的功能,並不想構建一套完整的對象來測試它。 FactoryGirl將如何幫助您? – agbodike

相關問題