user3291025
user3291025

Reputation: 1117

Rails: Routing error testing a controller's update method

I am working on learning TDD and am getting a routing error in my controller spec on the update method that I can't seem to fix:

PeopleController#update when a person is valid redirects to person
 Failure/Error: put :update, person: { first_name: "Joe" }
 ActionController::UrlGenerationError:
   No route matches {:action=>"update", :controller=>"people", :person=>{:first_name=>"Joe"}}

people_controller.rb

def edit
end

def update
  if @person.update(person_attributes)
    redirect_to @person, notice: 'Person was successfully updated.'
  else
    render :edit
  end
end

people_controller_spec.rb

describe "#update" do
  context "when a person is valid" do
    it "redirects to person" do
      person = Person.create(first_name: "Bob")
      allow(person).to receive(:update).and_return(true)
      allow(Person).to receive(:update).
        with(first_name: "Joe").
        and_return(person)

      put :update, person: { first_name: "Joe" } 
      expect(response).to redirect_to(person_path(person))
    end
  end
end

routes.rb

Rails.application.routes.draw do
  resources :people, only: [:new, :create, :show, :edit, :update]
end

How do you suggest testing this controller action?

Thanks.

Upvotes: 0

Views: 288

Answers (1)

Alejandro Babio
Alejandro Babio

Reputation: 5229

At people_controller_spec.rb change put :update, person: { first_name: "Joe" } with put :update, id: person.id, person: { first_name: "Joe" }

This must do the trick.

Upvotes: 1

Related Questions