Rspec:如何正确地发出补丁请求

时间:2017-08-23 20:34:29

标签: ruby-on-rails ruby testing 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)>'

我不明白为什么这不起作用。我已经尝试了很多不同的方法来制作补丁请求,似乎无法弄清楚如何让这个测试工作。再说一次,我确信这很简单。如果我留下任何重要信息,请告诉我。感谢

3 个答案:

答案 0 :(得分:4)

好的想通了。首先,我为了这个习惯而建立了一个工厂,以便清理。我一直搞乱语法并得出了正确的答案。

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

答案 1 :(得分:1)

我只使用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的{​​{1}}方法,请您使用habits的相应输出更新您的答案?

答案 2 :(得分:0)

我已经使用资源来生成路线,但是为了运行更新,我在Rails 4.2上做了如下操作

@current_habit = Habit.first

form_params = {
    name: "new",
    description: "shit",
  }

patch :update, {id: @current_habit.id, params: form_params}