简体   繁体   English

通过rspec factory_girl控制器测试上的自定义属性

[英]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: 到目前为止,这是我在pointings_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

But those paramaters dont seem to be reaching any method. 但是那些参数似乎没有达到任何方法。

Thanks in advamce 提前感谢

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'] 您在控制器操作中具有params["start_hour"]params["start_minutes"] ,但在规范中,您正在传递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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM