2017-09-17 8 views
2

次の操作を試しましたが、編集ページにリンクしている編集アイコンがインデックスページの記事の所有者に表示されていることを検出できません。インデックスページで編集アイコンが表示されている場合のRspecテスト

article_spec.rb

describe 'navigate' do 
    before do 
    @user = FactoryGirl.create(:user) 
    login_as(@user, :scope => :user) 
    end 

    describe 'edit' do 
    before do 
     @edit_user = User.create(name: "asdf", email: "[email protected]", password: "asdfasdf", password_confirmation: "asdfasdf") 
     login_as(@edit_user, :scope => :user) 
     @edit_post = Article.create(title:"Post to edit", description: "asdf", user_id: @edit_user.id) 
    end 

    it 'can be reached by clicking edit on index page' do 
     visit articles_path 
     visit "/articles/#{@edit_post.friendly_id}/edit" 
     expect(page.status_code).to eq(200) 
    end 

    it 'edit icon is visible to article owner' do 
     visit articles_path 
     expect(page.status_code).to eq(200) 
     link = "a[href = '/articles/#{@edit_post.friendly_id}/edit']" 
     expect(page).to have_link(link) 
    end 
end 

障害:

1) navigate edit edit icon is visible to article owner 
    Failure/Error: expect(page).to have_link(link) 
     expected to find link "a[href = '/articles/post-to-edit/edit']" but there were no matches 
    # ./spec/features/article_spec.rb:69:in `block (3 levels) in <top (required)>' 

記事/ index.html.erb

<% @articles.each do |article| %> 
    <%= render 'article', article: article %> 
    <% end %> 

<% if current_user == article.user %> 
    <%= link_to edit_article_path(article), class: "btn btn-xs btn-default" do %> 
     <i class="glyphicon glyphicon-pencil"></i> 
    <% end %> 
    <% end %> 

_article.html.erb私はfriendly_id宝石を使用するので、記事のタイトルは、そのURLに含まれています。

答えて

0

あなたは次のように、全体ではなく、リンクオブジェクトを渡し、あなたのタグとhave_linkマッチャーへのHREFの目に見える「内側」コンテンツを渡して試すことができます:

expect(page).to have_link(nil, href: "/articles/#{@edit_post.friendly_id}/edit") 

あなたは見えていないとして生成されたアンカータグ内のコンテンツはゼロにすることができ、この方法でhrefはlinkのものと一致します。

あなたがタグ「全体」としてリンク変数を定義してきたように、これはhave_cssマッチャで使用することができ、また、動作するはずです:

expect(page).to have_css link 

あなたはHREFを生成する方法を簡素化することができ対応するパスを使用する属性(edit_article_path(@edit_post)など)

関連する問題