gpt4 book ai didi

javascript - capybara 无法找到链接 "Delete Comment"

转载 作者:行者123 更新时间:2023-12-03 05:13:58 24 4
gpt4 key购买 nike

这是 View #views/comments/_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>

这是测试...#spec/features/03_comments_spec.rb

require 'spec_helper'
require '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 文件将其删除。

#views/comments/destroy.js.erb

$('.comment').remove()

所以我安装了phantomjs

#support/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

#spec/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"

最佳答案

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

关于javascript - capybara 无法找到链接 "Delete Comment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com