RSPEC测试是否在索引页面上可见编辑图标



我尝试执行以下操作,但是测试无法检测到索引所有者在索引页面上可见到编辑页面的编辑图标。

ARTICT_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

失败:

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

ARTICES/index.html.erb

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

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

我使用友好的gem,因此文章的标题包含在其URL中。

您可以尝试将A标签的可见"内部"内容及其HREF传递给has_link Matcher,而是传递整个链接对象,例如:

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

由于您在生成的锚固标签中没有可见的内容,因此可以是零,因此,这样的方式HREF在link中与您的匹配。

当您将链接变量定义为"整个"标签时,可以与HAS_CSS Matcher一起使用,并且应该使用:

expect(page).to have_css link

您可以使用相应的路径来简化生成HREF属性的方式,例如edit_article_path(@edit_post)

最新更新