2017-09-08 8 views
0

rspecテストを実行しているときにこのエラーに直面しています。私はclick_link "Title 1"を試して、変数にArticle.createを割り当てましたが、同じエラーが表示されます。RSpecの失敗/エラー:click_link article.title

Failure/Error: click_link article.title 

Capybara::ElementNotFound: 
    Unable to find link "Title 1" 

comment_spec.rb

require "rails_helper" 

describe 'navigate' do 
    let(:user) { FactoryGirl.create(:user) } 

    let(:article) do 
    Article.create(title: "Title 1", description: "Some description", user_id: user.id) 
    end 

    before do 
    login_as(user, :scope => :user) 
    end 

    describe 'create' do 
    before do 
     visit articles_path 
    end 

    it"permits a signed in user to write a review" do 
     click_link article.title 
     fill_in "Content", with: "An awesome article" 
     click_button "Post" 
     expect(page).to have_content("An awesome article") 
     expect(current_path).to eq(article_path(article.id)) 
    end 
    end 
end 

スペック/工場/ user.rb

FactoryGirl.define do 
    sequence :email do |n| 
    "test#{n}@example.com" 
    end 

    factory :user do 
    name 'Tester' 
    email { generate :email } 
    password "asdfasdf" 
    password_confirmation "asdfasdf" 
    end 

EDIT:

記事/ index.html.erb

 <div class="tab-pane active" id="tab1"> 
     <% @articles.each do |article| %> 
      <%= render 'article', article: article %> 
     <% end %> 
     </div> 

_article.html.erb

<%= link_to article.title, article_path(article) %> 

答えて

0

letは、あなたがそれを呼び出すまでブロックが評価されていないことを意味遅延ロードです。

require "rails_helper" 

describe 'navigate' do 
    let(:user) { FactoryGirl.create(:user) } 
    let!(:article) do 
    Article.create(title: "Title 1", description: "Some description", user_id: user.id) 
    end 

    before do 
    login_as(user, :scope => :user) 
    end 

    describe 'create' do 
    before do 
     visit articles_path 
    end 

    it"permits a signed in user to write a review" do 
     click_link article.title 
     fill_in "Content", with: "An awesome article" 
     click_button "Post" 
     expect(page).to have_content("An awesome article") 
     expect(current_path).to eq(article_path(article.id)) 
    end 
    end 
end 
+0

同じエラーがまだ聞かせて使用しているにもかかわらず表示されます:

before do article # calls the block visit articles_path end 

代替は遅延ロードされていないlet!を使用することです! – doyz

関連する問題