在rspec factory_girl控制器测试上传递自定义属性

时间:2014-12-30 23:43:19

标签: ruby-on-rails rspec automated-tests factory-bot

我正在尝试测试以下创建方法:

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

我的问题是我不知道如何将这些数据作为参数添加到我的测试中:

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

到目前为止,这是我在appointmentments_controller_spec.rb上的内容:

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

但那些参与者似乎没有达到任何方法。

谢谢你的推荐

1 个答案:

答案 0 :(得分:1)

您在控制器操作中有params["start_hour"]params["start_minutes"],但在规范中,您正在传递params['appointment']['params']['start_hour'] params['appointment']['params']['start_minutes']

假设您的控制器操作是您想要的,请在规范中尝试:

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}