Capybara test failing with AJAX response

ⅰ亾dé卋堺 提交于 2020-01-05 10:30:19

问题


I have the following Capybara test, which should click through and change the content of a comment. The issue is that the content form is loaded into a modal that pops up upon clicking the edit button, and my modal isn't being rendered in the test. (This functionality works in the app). save_and_open_page opens a page containing just the json object.

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

回答1:


The capybara test needed to have the option js: true passed into the descrbe block.

Here is the result:

  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


来源:https://stackoverflow.com/questions/18544142/capybara-test-failing-with-ajax-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!