下面是视图#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_leaner.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"
转载请注明出处:http://www.shenkehuoyun.com/article/20230526/2481075.html