Reputation: 77
I am having difficulty getting a test to pass:
Error:
Failure/Error: post :create, { appointment: { dentist_id: '1', patient_id: '2', appt_date: '2015-12-05 09:00:00' } }
ActiveRecord::RecordNotFound:
Couldn't find Dentist with 'id'=
Appointment Controller:
def create
@dentist = Dentist.find(params[:dentist_id])
@appointment = @dentist.appointments.new(appointment)
if Appointment.exists?(:appt_date => @appointment.appt_date)
render :new
else
@appointment.save
redirect_to dentist_path(@dentist)
end
end
Appointment Spec:
describe 'POST #create' do
it 'assigns a newly created appointment as @appointment' do
post :create, { appointment: { dentist_id: '1', patient_id: '2', appt_date: '2015-12-05 09:00:00' } }
expect(assigns(:appointment)).to be_a(Appointment)
expect(assigns(:appointment)).to be_persisted
end
end
What am I missing? Any help would be most appreciated.
Upvotes: 0
Views: 1868
Reputation: 3284
In your request, you're doing post :create, { appointment: { dentist_id: '1', ... } }
, which means that params[:dentist_id]
in your controller will be nil
(notice how you're nesting dentist_id
under appointment
in your request params). To fix this, change @dentist = Dentist.find(params[:dentist_id])
in your controller action to @dentist = Dentist.find(params[:appointment][:dentist_id])
, or change your request to post :create, { dentist_id: '1', appointment: { patient_id: '2' ... } }
.
Update
You'll also need to create a Dentist
record in your database before the test is run. To do that, add something like this to your test case:
describe 'POST #create' do
before { Dentist.create!(id: 1) }
it 'assigns a newly created appointment as @appointment' do
post :create, { dentist_id: '1', appointment: { patient_id: '2', appt_date: '2015-12-05 09:00:00' } }
...
end
end
Upvotes: 1