[英]rails+rspec+capybara testing edit resource
我有一個測試用例:
#spec/features/post_spec.rb
require 'spec_helper'
describe "Posts" do
subject { page }
describe "edit" do
post=FactoryGirl.create(:post)
puts post.valid?
puts post_path(post.id)
puts post.id
before { visit edit_post_path(post.id) }
it {should have_content('Editing')}
it {current_path.should == edit_post_path(post.id)}
end
end
我對通過路由系統生成URL有問題:
$rspec spec/features/posts_spec.rb
true
81
FF
Failures:
1) Posts edit
Failure/Error: before { visit edit_post_path(post.id) }
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"posts", :locale=>81, :id=>nil, :format=>nil} missing required keys: [:id]
# ./spec/features/2posts_spec.rb:13:in `block (3 levels) in <top (required)>'
2) Posts edit
Failure/Error: before { visit edit_post_path(post.id) }
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"posts", :locale=>81, :id=>nil, :format=>nil} missing required keys: [:id]
# ./spec/features/2posts_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.00592 seconds
2 examples, 2 failures
Failed examples:
rspec ./spec/features/2posts_spec.rb:15 # Posts edit
rspec ./spec/features/2posts_spec.rb:16 # Posts edit
Randomized with seed 6503
如您所見,在生成鏈接中不存在id
參數,而locale參數具有id
參數! 我不知道。 這是非常無人參與的行為。
更新:我試圖在edit_post_path
使用post
代替post.id
結果如下:
$rspec spec/features/posts_spec.rb
true
83
FF
Failures:
1) Posts edit
Failure/Error: before { visit edit_post_path(post) }
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"posts", :locale=>#<Post id: 83, title: "Vel Voluptas Veniam Ea Neque", autor: "Christina Rogahn", img_path: "Ut Iste Dolore", body: "Adipisci cupiditate eum eum deleniti facilis. Itaqu...", created_at: "2013-10-28 17:26:30", updated_at: "2013-10-28 17:26:31", email: "candelario@abernathy.name", user_id: 1>, :id=>nil, :format=>nil} missing required keys: [:id]
# ./spec/features/2posts_spec.rb:13:in `block (3 levels) in <top (required)>'
2) Posts edit
Failure/Error: before { visit edit_post_path(post) }
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"posts", :locale=>#<Post id: 83, title: "Vel Voluptas Veniam Ea Neque", autor: "Christina Rogahn", img_path: "Ut Iste Dolore", body: "Adipisci cupiditate eum eum deleniti facilis. Itaqu...", created_at: "2013-10-28 17:26:30", updated_at: "2013-10-28 17:26:31", email: "candelario@abernathy.name", user_id: 1>, :id=>nil, :format=>nil} missing required keys: [:id]
# ./spec/features/2posts_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.00532 seconds
2 examples, 2 failures
Failed examples:
rspec ./spec/features/2posts_spec.rb:14 # Posts edit
rspec ./spec/features/2posts_spec.rb:15 # Posts edit
Randomized with seed 32039
這是我的routes.rb文件:
#config/routes.rb
SitoNegozio::Application.routes.draw do
scope "(:locale)", locale: /it|en/ do
devise_for :users , controllers: {omniauth_callbacks: "users/omniauth_callbacks"}
resources :users
resources :posts do
resources :msgs
end
root :to => 'static_pages#index'
end
end
解決了:
#https://github.com/rspec/rspec-rails/issues/255#issuecomment-24698753
config.before(:each, type: :feature) do
default_url_options[:locale] = I18n.default_locale
end
對於任何遇到類似錯誤的人。
還值得注意的是,如果您未在控制器中設置編輯方法的實例,則可能會missing required keys: [:id]
,因此應如下所示:
def edit
@post = Post.find(params[:id])
end
您的功能方法將類似於:
background do
@post = Post.create!(:name => 'Testing')
end
visit edit_post_path(@post)
fill_in 'post_name', with: 'Testing Article'
click_button 'Update Post'
您的視圖如下所示:
<%= form_for :post, url: post_path(@post), method: :patch do |form| %>
<%= @post.errors.full_messages %>
<%= form.label :name %>
<%= form.text_field :name %>
<%= form.submit "Update Post" %>
<% end %>
而不是做
visit edit_post_path(post.id)
刪除.id
並嘗試執行以下操作:
visit edit_post_path(post)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.