简体   繁体   中英

rails+rspec+capybara testing edit resource

I have a test case:

#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

I have a problem with the generation of URL by route system:

$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

As you can see, the id param is not present in the generate link, and the locale param have the id param! I have no idea. This is very Unattended behavior.

UPDATE: I have tried to use post instead post.id in edit_post_path . This is the result:

$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

this is my routes.rb file:

#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

SOLVED:

#https://github.com/rspec/rspec-rails/issues/255#issuecomment-24698753
  config.before(:each, type: :feature) do
    default_url_options[:locale] = I18n.default_locale
  end

For anyone getting similar errors.

It is also worth noting that you could get missing required keys: [:id] if you don't set your edit method's instance in your controller, so it should be like so:

def edit
  @post = Post.find(params[:id])
end

And your feature method would look something like:

background do
  @post = Post.create!(:name => 'Testing')
end

visit edit_post_path(@post)
fill_in 'post_name', with: 'Testing Article'
click_button 'Update Post'

Your view would look something like so:

<%= 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 %>

Instead of doing,

visit edit_post_path(post.id)

Remove the .id and try doing:

visit edit_post_path(post)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM