RubyonRailsのハッシュに新しいキーと値を追加する際に問題が発生しました。メソッドは2つのデバッグ出力でこのようになり、後で正しいIDを持つプロバイダーにアクセスするために、値としてインデックス+1のプロバイダーキーを追加する必要があります。
search_result.each_with_index do |articles, index|
puts "merge-articles: #{articles}"
articles.each{ |article| article[:provider] = index + 1;}
puts "merge-articles(later): #{articles}"
end
私はプットからそれらの出力を取得しますが、これは私の心には非常によく見えます:
merge-articles: [{:ean=>"9780234474278", :author=>"Dan Brown", :name=>"The Da Vinci Code", :price=>19.65, :image=>"www.google.de/image.png"}]
merge-articles(later): [{:ean=>"9780234474278", :author=>"Dan Brown", :name=>"The Da Vinci Code", :price=>19.65, :image=>"www.google.de/image.png", :provider=>2}]
キーが存在するかどうかのみをテストする仕様では、次のエラーが発生します。
Failure/Error: HomeController.merge(@no_same_items_merge).each do |item|
TypeError:
can't convert Symbol into Integer
# ./app/controllers/home_controller.rb:41:in `[]='
# ./app/controllers/home_controller.rb:41:in `block (2 levels) in merge'
# ./app/controllers/home_controller.rb:41:in `each'
# ./app/controllers/home_controller.rb:41:in `block in merge'
# ./app/controllers/home_controller.rb:39:in `each'
# ./app/controllers/home_controller.rb:39:in `each_with_index'
# ./app/controllers/home_controller.rb:39:in `merge'
# ./spec/controllers/home_controller_spec.rb:104:in `block (5 levels) in <top (required)>'
編集:RSpecテストは次のようになります。
it "should return an array of right formatted hashes" do
HomeController.merge(@no_same_items_merge).each do |item|
item.should have_key(:name)
item.should have_key(:ean)
item.should have_key(:author)
item.should have_key(:description)
item.should have_key(:url)
item.should have_key(:prices)
item.should have_key(:images)
end
end
ご協力いただきありがとうございます!