0
votes

I'm still fairly new to the rspec way. I've read many posts on a redirect_to but cannot seem to get passed this error. I'm trying to simulate the passing of form variable/value in my rspec test, but getting a routing error. I'm converting tests to rspec tests on an app that is live and working.

In my employees_micros_controller.rb, I have an update method that expects params[:employees_micro][:id]. My question is How do I simulate this in my rspec test?

Controller:

def update
  @em = nil
  # check for id from form
  if params[:employees_micro][:id]
    @em = EmployeesMicro.find_by_id(params[:employees_micro][:id])
  end
  ....
end

Rspec Test: note: ### error line ###

# update and redirect
describe 'POST #update' do
  it "updates employee_micro and redirect to employee_details" do
    # @emp_micros set in before(each) above
    @emp_micros.home_job = 123
    # update method looks for params[:employees_micro][:id]
    post :update, :employees_micro => { :id => @emp_micros } ### error line ###
    expect(response).to redirect_to("employee_details_employee_path")
  end
end

Error:

Failure/Error: post :update, :employees_micro => { :id => @emp_micros }
 ActionController::RoutingError:
   No route matches {:employees_micro=>{:id=>"11960"}, :controller=>"employees_micros", :action=>"update"}

Is my >> post :update line syntax correct?

I do not understand the routing error. I'm only trying to simulate passing a form variable to the update method from my test. If I remove everything after the post/put line, I still get the same error, so it is definitely on that line.

I've also tried using "put" in place of "post" in the test. It nets me the same error.

Thanks for any tips or advice.

1

1 Answers

0
votes

The update route expects the :id param to be at the top level, so use:

post :update, :id => @emp_micros, :employees_micro => { <other attributes to update> }