How Would You Generate Hash with Nested Attributes for Rspec Controller #create Test

Viewed 1142

I am testing Rails 4 controller #create action in Rspec as following:

describe "POST #create" do    
    it "saves personality test responses in database" do
      expect {
        post :create, graduate_id: graduate, personality_test: attributes_for(...)
      }.to change(PersonalityTest, :count).by(1)
    end
end

Note, the attributes_for(...) above is the part I am struggling with. In order for the controller to create a new record, I need to pass a hash in this format (where "personality_test_template_question_id" and "personality_test_template_answer_id" are foreign keys to existing models:

"personality_test"=>{
  "personality_test_template_id"=>"1", "personality_test_responses_attributes"=>{
    "0"=>{"personality_test_template_question_id"=>"1", "personality_test_template_answer_id"=>"2"}, 
    "1"=>{"personality_test_template_question_id"=>"2", "personality_test_template_answer_id"=>"9"}, 
    "2"=>{"personality_test_template_question_id"=>"3", "personality_test_template_answer_id"=>"14"}, 
    "3"=>{"personality_test_template_question_id"=>"4", "personality_test_template_answer_id"=>"21"}, 
    "4"=>{"personality_test_template_question_id"=>"5", "personality_test_template_answer_id"=>"27"}, 
    "5"=>{"personality_test_template_question_id"=>"6", "personality_test_template_answer_id"=>"33"}, 
    "6"=>{"personality_test_template_question_id"=>"7", "personality_test_template_answer_id"=>"38"}, 
    "7"=>{"personality_test_template_question_id"=>"8", "personality_test_template_answer_id"=>"46"}, 
    "8"=>{"personality_test_template_question_id"=>"9", "personality_test_template_answer_id"=>"54"}, 
    "9"=>{"personality_test_template_question_id"=>"10", "personality_test_template_answer_id"=>"58"}, 
    "10"=>{"personality_test_template_question_id"=>"11", "personality_test_template_answer_id"=>"64"}, 
    "11"=>{"personality_test_template_question_id"=>"12", "personality_test_template_answer_id"=>"70"}, 
    "12"=>{"personality_test_template_question_id"=>"13", "personality_test_template_answer_id"=>"76"}, 
    "13"=>{"personality_test_template_question_id"=>"14", "personality_test_template_answer_id"=>"83"}, 
    "14"=>{"personality_test_template_question_id"=>"15", "personality_test_template_answer_id"=>"86"}, 
    "15"=>{"personality_test_template_question_id"=>"16", "personality_test_template_answer_id"=>"92"}, 
    "16"=>{"personality_test_template_question_id"=>"17", "personality_test_template_answer_id"=>"101"}, 
    "17"=>{"personality_test_template_question_id"=>"18", "personality_test_template_answer_id"=>"107"}, 
    "18"=>{"personality_test_template_question_id"=>"19", "personality_test_template_answer_id"=>"110"}, 
    "19"=>{"personality_test_template_question_id"=>"20", "personality_test_template_answer_id"=>"119"}, 
    "20"=>{"personality_test_template_question_id"=>"21", "personality_test_template_answer_id"=>"122"}, 
    "21"=>{"personality_test_template_question_id"=>"22", "personality_test_template_answer_id"=>"132"}, 
    "22"=>{"personality_test_template_question_id"=>"23", "personality_test_template_answer_id"=>"137"}, 
    "23"=>{"personality_test_template_question_id"=>"24", "personality_test_template_answer_id"=>"140"}, 
    "24"=>{"personality_test_template_question_id"=>"25", "personality_test_template_answer_id"=>"146"}, 
    "25"=>{"personality_test_template_question_id"=>"26", "personality_test_template_answer_id"=>"152"}, 
    "26"=>{"personality_test_template_question_id"=>"27", "personality_test_template_answer_id"=>"162"}, 
    "27"=>{"personality_test_template_question_id"=>"28", "personality_test_template_answer_id"=>"165"}, 
    "28"=>{"personality_test_template_question_id"=>"29", "personality_test_template_answer_id"=>"173"}, 
    "29"=>{"personality_test_template_question_id"=>"30", "personality_test_template_answer_id"=>"177"}
  }
}

I am not clear what would be the proper way to generate a hash in such format. I've tried FactoryGirl attributes_for method, but it doesn't generate attributes for the nested factories. Any tips will be highly appreciated.

Thanks!

1 Answers
Related