Ruby on rails Rspec:如何正确地发出补丁请求
我一直在寻找能解释我所遇到问题的东西。我可能错过了一些简单的东西,所以我希望有人能抓住我的错误Ruby on rails Rspec:如何正确地发出补丁请求,ruby-on-rails,ruby,testing,rspec,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' resour
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
HabitController:
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
HabitControllerSpec:
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)>'
1)HabitsController#更新应更新习惯名称/描述
失败/错误:修补程序“习惯/1”,表单参数
ActionController::UrlGenerationError:
没有路由匹配{:action=>“habits/1”,:controller=>“habits”,
:description=>“shit”,:name=>“new”}
#./spec/controller/habits\u controller\u spec.rb:85:in`block(3
级别)在'
我不明白这为什么不起作用。我尝试了很多不同的方法来提出补丁请求,但似乎不知道如何让这个测试工作。再说一次,我相信这很简单。如果我遗漏了任何重要信息,请告诉我。谢谢我只使用了,但我很确定它会被使用,或者语法会相同或相似
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
创建一个对象,您应该直接保存该对象
其次,由于错误,您似乎没有针对
习惯的修补程序方法,请您使用rails routes
的相应输出更新您的答案好吗?好的,我知道了。首先,我为这个习惯建立了一个工厂,只是为了把事情弄清楚。我不断弄乱语法,得出了正确的答案
form_params = {
params: {
name: "new",
description: "shit"
}
}
patch :update,id: 1, form_params
这告诉我错误的参数数量,我最终意识到我需要用我的form_参数传递id。就像我说的,小错误
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
正确代码:
form_params = {
id: 1,
name: "new",
description: "shit",
}
patch :update, params: form_params
我使用了资源来生成路由,但为了运行更新,我使用rails 4.2做了如下操作
@current_habit = Habit.first
form_params = {
name: "new",
description: "shit",
}
patch :update, {id: @current_habit.id, params: form_params}
请注意,如果要使用显式Rails路由,可以这样调用它:patch habity\u path(1,form\u params)
。请注意,您需要使用资源名称的单数形式,而不是复数形式,因为您正在修补单个资源。i、 e.不要这样做:patch habits\u path(1,form\u params)