mercredi 23 août 2017

Rspec: How to properly make patch request

I've been searching all over for something that would explain the problem I'm having. I am probably missing something simple so I hope someone can catch my mistake.

Rails.application.routes.draw do

 get 'auth/:provider/callback', to: 'sessions#create', as: 'login'
 get 'auth/failure', to: redirect('/')
 get 'signout', to: 'sessions#destroy', as: 'signout'

 resources :sessions, only: [:create, :destroy]
 resources :home, only: [:show]
 resources :static_pages, only: [:show]
 resources :habits
 root to: "home#show"
 get '/started' => 'home#started'

end

HabitsController:

def update
  if params[:name].present? && params[:description].present?
    Habit.habit_edit(@current_habit, form_params)
    flash[:success] = "Edited habit: " + @current_habit.name + '!'
    redirect_to habit_path(:id => session[:user_id])
  else
    flash[:notice] = "Habit must have a name and a description"
    redirect_to edit_habit_path(:id => @current_habit.id)
  end
end

HabitsControllerSpec:

describe "#update" do

it "should update the habit name/description" do
  form_params = {
    params: {
    name: "hello",
    description: "hello"
    }
  }
  post :create, form_params
  @current_habit = Habit.first
  form_params = {
    params: {
    name: "new",
    description: "shit"
    }
  }
  **patch "habits/1", form_params**


end

Problem:

1) HabitsController#update should update the habit name/description
 Failure/Error: patch "habits/1", form_params

 ActionController::UrlGenerationError:
   No route matches {:action=>"habits/1", :controller=>"habits", 
:description=>"shit", :name=>"new"}
 # ./spec/controllers/habits_controller_spec.rb:85:in `block (3 
levels) in <top (required)>'

I don't understand why this isn't working. I've tried lots of different methods to make a patch request and can't seem to figure out how to get this test to work. Again, I'm sure it is simple. If I left any important info out let me know. Thanks

Aucun commentaire:

Enregistrer un commentaire