【问题标题】:rails+rspec+capybara testing edit resourcerails+rspec+capybara 测试编辑资源
【发布时间】:2013-11-07 12:49:06
【问题描述】:

我有一个测试用例:

#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 参数,而语言环境参数具有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

【问题讨论】:

  • 请发布您的路线文件的相关部分。

标签: ruby-on-rails ruby rspec


【解决方案1】:

对于遇到类似错误的任何人。

另外值得注意的是,如果你没有在控制器中设置编辑方法的实例,你可能会得到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 %>

【讨论】:

    【解决方案2】:

    而不是做,

    visit edit_post_path(post.id)
    

    删除.id 并尝试这样做:

    visit edit_post_path(post)
    

    【讨论】:

    • 我刚试过,结果是一样的:在params locale中我有完整的post对象
    • 是同样的错误。现在我将通过编辑原始帖子向您展示错误
    • 你也可以发布你的 routes.rb 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多