1

我有以下 Capybara 测试,它应该点击并更改评论的内容。问题是内容表单被加载到单击编辑按钮时弹出的模态中,并且我的模态没有在测试中呈现。(此功能在应用程序中有效)。save_and_open_page 打开一个只包含 json 对象的页面。

特征规范.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();
});

评论控制器.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
4

1 回答 1

1

capybara 测试需要将选项 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
于 2013-08-31T20:46:42.280 回答