【问题标题】:Capybara redirecting to JSON responseCapybara 重定向到 JSON 响应
【发布时间】:2015-10-07 19:28:39
【问题描述】:

我的 Rails 应用程序中有一个表单,可以将数据远程发送到控制器操作。然后有 JavaScript 等待成功的 AJAX 响应,然后更新页面的各个部分。

使用 Turnip、Rspec 和 Capybara 进行测试时,current_page 被重定向到 JSON 响应,而不是像我预期的那样停留在页面上。

以下是部分代码:

被击中的控制器动作

def update
  @conversation.update(conversation_params)
  @conversation.save
  render json: @conversation, serializer: ConversationSerializer
end

表格

= simple_form_for(convo, remote: true, method: :json) do |f|
  - f.fields_for :messages, Message.new(conversation: f.object) do |msg|
    = msg.input :text, label: 'Message'
    = msg.submit 'Post Message', id: 'message-submit'

Coffeescript 监听成功

$(document).on 'ajax:success', 'form[data-remote]', (xhr, data, status) ->
  new_message_id = data.conversation.conversation_messages.pop().id

  $.get '/conversation_messages/'+new_message_id+'/partial', (data) ->
    $('#conversation .message').last().after(data)

$(document).on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
  location.reload()

$(document).on 'ajax:success', 'form[data-remote]', (xhr, data, status) ->
  location.reload()

显示错误内容的测试

step 'I say :message' do |message|
  fill_in 'Message', with: message
  click_on 'Post Message'
end

step 'I see the conversation message :message from :username' do |message, username|
  expect(page).to have_selector('.conversation-message-sender', text: username)
  expect(page).to have_selector('.conversation-message-text', text: message)
end

【问题讨论】:

    标签: javascript ruby-on-rails ajax rspec capybara


    【解决方案1】:

    您需要使用支持 JavaScript 的驱动程序才能进行 ajax 提交 - 请参阅 https://github.com/jnicklas/capybara#drivers

    【讨论】:

    • 谢谢。这绝对是答案,但最终当然不是我真正的问题。我会接受并编辑我的帖子,使其仅包含相关内容
    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2013-12-04
    • 2016-07-20
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2010-09-11
    相关资源
    最近更新 更多