Как настроить систему Rspec, чтобы широковещательные сообщения ActionCable появлялись в представлении - PullRequest
0 голосов
/ 30 декабря 2018

Я хочу проверить, что сообщения, которые передаются по завершении некоторых фоновых заданий, действительно отображаются в представлении.

У меня есть модульные тесты, которые работают нормально.На самом деле я хотел бы убедиться, что 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

Есть идеи, как это сделать?

...