2017-08-23 12 views
1

私は、私が抱えている問題を説明する何かを探してきました。私はおそらく単純なものを見逃しているので、誰かが私の間違いを捉えることを願っている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)>' 

なぜこれが機能しないのかわかりません。私はパッチのリクエストをするためにさまざまな方法を試しましたが、このテストをどのように動作させるかを理解できないようです。再び、私はそれが簡単だと確信しています。私が重要な情報を残したら、私に知らせてください。ありがとう

答えて

2

わかりました。まず、物事をクリアするための習慣のための工場を作った。私は構文を乱し続け、正しい答えになった。

form_params = { 
    params: { 
    name: "new", 
    description: "shit" 
    } 
} 
patch :update,id: 1, form_params 

これは私の引数の間違った番号を言っていたし、私は最終的に、私は私のform_paramsでIDを渡すために必要な実現。私が言ったように、小さな間違い。

正しいコード:

form_params = { 
    id: 1, 
    name: "new", 
    description: "shit", 
    } 
    patch :update, params: form_params 
0

私はたったrack-testを使用しましたが、私はそれが使用されるか構文が同じか類似していると確信しています。

context 'update fields' do 
    let(:payload) { 
     { 
     field: value 
     } 
    } 
    Given do 
     repository.save(resource: resource_object) 
    end 
    When do 
     patch('/resources/123', payload.to_json) 
    end 

    let(:result) { JSON.parse(last_response.body) } 

    Then { expect(last_response.status).to eq(400) } 
    end 

だから、私はあなたのコードに気づいた最初の事は、あなたが直接保存する必要がある場合には、オブジェクトを作成するためにPOSTを使用しています。

PATCHの方法はありません。habitsの場合は、rails routesの出力を更新してください。

関連する問題