2013-10-28 27 views
2

我有一個測試案例:軌+ rspec的+水豚測試編輯資源

#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! 我不知道。這是非常無人值守的行爲。

更新: 我曾嘗試使用post代替post.idedit_post_path。 這是結果:

$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: "[email protected]", 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: "[email protected]", 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 
+0

請張貼relev螞蟻你的路線文件的一部分。 – jvperrin

回答

0

而不是做,

visit edit_post_path(post.id) 

取出.id和嘗試做:

visit edit_post_path(post) 
+0

我剛剛嘗試過,結果是一樣的:在參數區域設置中,我有完整的發佈對象 – user1066183

+0

您是否仍然遇到無路由匹配錯誤? – Brock90

+0

是同樣的錯誤。現在我打算通過編輯原始文章向您顯示錯誤 – user1066183

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