Capybara无法找到链接“删除评论”

时间:2017-01-16 21:30:44

标签: javascript ruby-on-rails rspec phantomjs capybara

这是视图

的观点/评论/ _comment.html.erb

<div class="comment clearfix">

  <div class="comment_content">
    <p class="comment_name"><strong><%= comment.name %></strong></p>
    <p class="comment_body"><%= comment.body %></p>
    <p class="comment_time"><%= time_ago_in_words(comment.created_at) %> ago</p>
  </div>

  <% if user_signed_in? && current_user.email == ENV['ADMIN'] %>
    <p><%= link_to 'Delete Comment', [comment.post, comment], method: :delete, class: 'button', data: { confirm: 'Are you sure?' }, remote: true %></p>
  <% end %>

</div>

这是测试...

规格/特征/ 03_comments_spec.rb

要求'spec_helper' 需要'rails_helper'

feature 'blog posts', %Q{
  As an unauthenticated user
  I want to create and delete comments
} do

  let!(:post) { FactoryGirl.create(:post) }
  let!(:comment) { FactoryGirl.create(:comment, post: post) }

  scenario 'delete post comment', js: true do
    visit post_path(post)
    click_link 'Delete Comment'

    expect(page).to_not have_content(comment.name)
    expect(page).to_not have_content(comment.body)
  end
end

我认为可能需要检测javascript,因为我已使用destroy.js.erb文件将其删除。

的观点/评论/ destroy.js.erb

$('.comment').remove()

所以我安装了phantomjs

支持/ database_cleaner.rb

RSpec.configure do |config|
  config.before(:each) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.after(:each) do
  end
end

规格/ rails_helper.rb

require "capybara/poltergeist" 
Capybara.javascript_driver = :poltergeist
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|    
  config.use_transactional_fixtures = false

当我运行rspec时,测试失败并显示以下输出

 Failure/Error: click_link 'Delete Comment'

 Capybara::ElementNotFound:
   Unable to find link "Delete Comment"

1 个答案:

答案 0 :(得分:1)

&#34;删除评论&#34;链接仅在用户登录时显示(if user_signed_in? && ...),但您的测试中没有任何地方登录用户 - 因此链接实际上不在页面上。