本文介绍了水豚测试失败,AJAX响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Capybara测试,该测试应单击并更改评论的内容.问题在于,内容表单已加载到单击编辑"按钮时会弹出的模式中,而我的模式未在测试中呈现. (此功能在应用程序中有效). save_and_open_page打开仅包含json对象的页面.

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

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

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

推荐答案

水豚测试需要将js:true选项传递到descrbe块中.

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

这是结果:

  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

这篇关于水豚测试失败,AJAX响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:39