我有以下的Capybara测试,应该点击并更改评论的内容。问题是内容表单被加载到一个模式中,该模态在单击编辑按钮时弹出,并且我的模态未在测试中呈现。 (此功能适用于应用程序)。 save_and_open_page打开一个只包含json对象的页面。
feature_spec.rb
require 'spec_helper'
describe 'Edit comment' do
let(:commented_post) { FactoryGirl.create(:post_with_comments) }
describe "when current_user is the comment's author" do
it 'should edit the comment content' do
visit post_path(commented_post)
within ("#comment-#{commented_post.comments.first.id}") do
click_on "edit"
end
Capybara.default_wait_time = 15
save_and_open_page
fill_in 'comment_content', with: 'No, this is the best comment'
click_on 'Edit Comment'
expect(page).to have_content('No, this is the best comment')
end
end
end
post.js
var EditForm = {
init: function() {
$('.get-edit-comment').on('ajax:success', this.showEditModal);
},
showEditModal: function(e, data) {
e.preventDefault();
$('.reveal-modal').html(data.edit_template);
$('.reveal-modal').foundation('reveal', 'open');
}
}
$(document).ready( function() {
EditForm.init();
});
comments_controller.rb
def edit
@post = Post.find_by_id(params[:post_id])
@comment = Comment.find_by_id(params[:id])
render :json => {
edit_template: render_to_string(:partial => 'comments/form',
:locals => {post: @post, comment: @comment})
}
end
def update
Comment.find_by_id(params[:id]).update(comment_params)
redirect_to post_path(params[:post_id])
end
答案 0 :(得分:1)
水豚测试需要将选项js:true传递到descrbe块。
结果如下:
describe "when current_user is the comment's author", js: true do
it 'should edit the comment content' do
visit post_path(commented_post)
within ("#comment-#{commented_post.comments.first.id}") do
click_on "edit"
end
fill_in 'comment_content', with: 'No, this is the best comment'
click_on 'Edit Comment'
expect(page).to have_content('No, this is the best comment')
end
end