简体   繁体   中英

Rspec test if edit icon is visible on index page

I have tried doing the following but test is not able to detect that the edit icon linking to the edit page is visible to the article owner on the index page.

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: "asdfasdf@asdf.com", 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

Failures:

  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)>'

articles/index.html.erb

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

_article.html.erb

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

I use the friendly_id gem, so the title of the articles are contained in their url.

You can try passing the visible "inner" content of your a tag and its href to the have_link matcher, instead passing the whole link object, like:

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

As you don't have a visible content inside the generated anchor tag, it can be nil, and this way the href would match with yours in link .

As you've defined the link variable as an "entire" a tag, this could be used with the have_css matcher, and also should work:

expect(page).to have_css link

You could simplify the way you generate the href attribute using the corresponding path, like edit_article_path(@edit_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