Spec failed for accept nested attribute

Viewed 17

I have model candidate, job_application.

Candidate.rb

has_one :job_application, dependent: :destroy
accepts_nested_attributes_for: job_application

candidate_controller.rb

  def create
    candidate = Candidate.new(candidate_params)
    candidate.job_application.applied_at = Date.today
    if candidate.save!
       puts "created"
    else
      puts "unable to create"
    end
  end

  def candidate_params
    params.require(:candidate).permit(:first_name, :last_name, :email,
                                      job_application_attributes: %i[ applied_at proposal],
  end

The create is successful with applied_at today's date.

The problem is in my controller spec.

        post :create, params: { candidate: { first_name: "Ram", last_name: "Dhakal", email: "dhakal@gmail.com",
                                job_application_attributes: [{  proposal: "testing", applied_at: Date.today}],
                                
                              }
      end

While running spec test I encounter error msg undefine method applied_at for nil class. I debug the error. It shows me job_applicant but job_applicant.job_application is nil(empty). failed my controller test.

While testing from postman I get the data and successfully created. How can I pass my controller test?

0 Answers
Related