如何Rspec系统在视图中显示ActionCable广播消息

时间:2018-12-30 04:37:57

标签: ruby-on-rails-5 rspec-rails actioncable

我要测试在某些后台作业完成时广播的消息是否实际出现在视图中。

我对此进行了单元测试,效果很好。我实际上想确保运行JS,以便使用正确的消息更新视图。

到目前为止,我还没有找到任何方法来执行此操作。

这是我要在其中添加对广播消息的期望的测试:

require 'rails_helper'
require 'sidekiq/testing'


RSpec.describe 'sending a quote request', js: true do

  let(:quote_request_form) { build(:quote_request_form) }

  before do 
    create(:job_rate, :proofreading)
    create(:proofreader_with_work_events)
  end

  it 'shows the user their quotation' do
    visit new_quote_request_path
    fill_in("quote_request_form_name", with: quote_request_form.name)
    fill_in("quote_request_form_email", with: quote_request_form.email)
    attach_file('customFile','/Users/mitchellgould/RailsProjects/ProvenWordNew/spec/test_documents/quote_request_form/1.docx', make_visible: true)
    click_on "Submit"
    Sidekiq::Testing.inline! do
      page.execute_script("$('#invisible-recaptcha-form').submit()")

      expect(current_path).to eq(quote_confirm_path)

      #add expectation here:
      expect(page).to have_content("Calculating Time Required")

      page.execute_script("window.location.pathname = '#{quotation_path(Quotation.first)}'")

      expect(current_path).to eq(quotation_path(Quotation.first))
      expect(page).to have_content("Here is your quotation")
    end
  end
end

这是我的.coffee文件:

$(document).on 'turbolinks:load', ->
  if $("meta[name='current_user']").length > 0
    App.notification = App.cable.subscriptions.create "NotificationChannel",
      connected: ->
        # Called when the subscription is ready for use on the server

      disconnected: ->
        # Called when the subscription has been terminated by the server

      received: (data) ->
        $('.background_message').html(data.content)
        if data.head == 302 && data.path
          window.location.pathname = data.path

  else if App.notification 
    App.quotation.unsubscribe()
    delete App.notification

这是完成消息后广播消息的后台作业之一:

class CreateParagraphDetailsJob < ApplicationJob
  queue_as :default

  after_perform :broadcast_message, :calculate_proofreading_job_duration

  def perform(document, proofreading_job_id, current_user_id)
    document.create_paragraph_details
  end

  private

  def calculate_proofreading_job_duration
    CalculateDurationJob.set(wait: 1.seconds).perform_later proofreading_job_id, current_user_id
  end

  def broadcast_message
    ActionCable.server.broadcast "notification_channel_user_#{current_user_id}", content: "Analyzed writing quality of paragraphs"
  end

  def document
    self.arguments.first
  end

  def proofreading_job_id
    self.arguments.second
  end

  def current_user_id
    self.arguments.last
  end
end

关于如何执行此操作的任何想法?

0 个答案:

没有答案