Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec测试是否在索引页上显示编辑图标_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails Rspec测试是否在索引页上显示编辑图标

Ruby on rails Rspec测试是否在索引页上显示编辑图标,ruby-on-rails,rspec,Ruby On Rails,Rspec,我已经尝试了以下操作,但测试无法检测到链接到编辑页面的编辑图标在索引页面上对文章所有者可见 文章\u规范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: "as

我已经尝试了以下操作,但测试无法检测到链接到编辑页面的编辑图标在索引页面上对文章所有者可见

文章\u规范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
   <% @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 %>
失败:

  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)>'
1)文章所有者可以看到导航编辑图标
失败/错误:期望(第页)。具有_链接(链接)
应找到链接“a[href=”/articles/post to edit/edit']”,但没有匹配项
#./spec/features/article_spec.rb:69:in'block(3层)in'
articles/index.html.erb

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

\u article.html.erb

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


我使用的是友好的\u id gem,因此文章的标题包含在它们的url中。

您可以尝试将a标记的可见“内部”内容及其href传递给have\u link matcher,而不是传递整个link对象,如:

expect(page).to have_link(nil, href: "/articles/#{@edit_post.friendly_id}/edit")
由于生成的锚标记中没有可见内容,因此可以为零,这样href将与您在
链接中的链接相匹配

由于您已将link变量定义为“完整的”a标记,因此这可以与have_css matcher一起使用,并且还应该起作用:

expect(page).to have_css link
您可以简化使用相应路径生成href属性的方式,如
edit\u article\u path(@edit\u post)