私は、私が抱えている問題を説明する何かを探してきました。私はおそらく単純なものを見逃しているので、誰かが私の間違いを捉えることを願っているRspec:パッチリクエストを正しく作成する方法
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
ルート:
habit GET /habits/:id(.:format) habits#show
PATCH /habits/:id(.:format) habits#update
PUT /habits/:id(.:format) habits#update
DELETE /habits/:id(.:format) habits#destroy
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
問題:
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)>'
なぜこれが機能しないのかわかりません。私はパッチのリクエストをするためにさまざまな方法を試しましたが、このテストをどのように動作させるかを理解できないようです。再び、私はそれが簡単だと確信しています。私が重要な情報を残したら、私に知らせてください。ありがとう