Search code examples
ruby-on-railsrspecautomated-testsfactory-bot

pass custom attribute on rspec factory_girl controller test


I'm trying to test the following create method:

def create
  @appointment = Appointment.new(appointment_params)
  set_start_end_time(params["start_hour"],params["start_minutes"])
  if @appointment.save
    respond_to do |format|
      format.js
    end
  end
end

def set_start_end_time(hours, minutes)
  service_duration = @appointment.service ? @appointment.service.duration : 30
  @appointment.start = @appointment.start.change({ hour: hours, min: minutes })
  @appointment.end = @appointment.start + ( service_duration * 60)
end

My problem is i don't know how to add this data as a parameter on my tests:

params["start_hour"], params["start_minutes"]

This is what i have on appointments_controller_spec.rb so far:

it "creates a new Appointment" do
    expect {
      post :create, {:appointment => { :client_phone => 123456, :start => "2014-12-09 11:00:00", :params => {"start_hour" => 11, "start_minutes" => 30}, :user_id => user.id, :service_id => @service.id }, :company_id => user.company.to_param, :user_id => user.id, format: :js}
    }.to change(Appointment, :count).by(1)
  end

But those parameters don't seem to be reaching any method.


Solution

  • You have params["start_hour"] and params["start_minutes"] in the controller action but in the spec, you're passing params['appointment']['params']['start_hour'] params['appointment']['params']['start_minutes']

    Assuming that your controller action is how you want it, try this in the spec:

    post :create, { :appointment => { :client_phone => 123456, :start => "2014-12-09 11:00:00" , :user_id => user.id, :service_id => @service.id }, :start_hour => 11, :start_minutes => 30, :company_id => user.company.to_param, format: :js}