这是视图
<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>
这是测试...
要求'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
文件将其删除。
$('.comment').remove()
所以我安装了phantomjs
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
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"
答案 0 :(得分:1)
&#34;删除评论&#34;链接仅在用户登录时显示(if user_signed_in? && ...
),但您的测试中没有任何地方登录用户 - 因此链接实际上不在页面上。