水豚找不到 link "Delete Comment"
Capybara unable to find link "Delete Comment"
这是风景
#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"
"Delete Comment" link 仅在用户登录时显示 (if user_signed_in? && ...
),但在您的测试中没有任何地方登录用户 - 因此 link 实际上不在页面上。
这是风景 #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"
"Delete Comment" link 仅在用户登录时显示 (if user_signed_in? && ...
),但在您的测试中没有任何地方登录用户 - 因此 link 实际上不在页面上。