Как использовать RSpec для тестирования сообщений ActiveJob, транслирующих сообщения по каналу пользователя? - PullRequest
0 голосов
/ 26 июня 2019

Я следую руководству RSpec о том, как проверить, что сообщения были переданы с использованием ActionCable.

Вот их пример:

RSpec.describe CommentsController do
  describe "POST #create" do
    expect { post :create, comment: { text: 'Cool!' } }.to
      have_broadcasted_to("comments").with(text: 'Cool!')
  end
end

Вот мой ActiveJob:

  def perform(document_id, proofreading_job_id, current_user_id)
    document.paragraph_details.destroy_all
    broadcast_message message: "Determining the exact amount of time to proofread your document"
    document.create_paragraph_details
    broadcast_message message: "Determining the earliest delivery date..."
    proofreading_job.set_duration
    broadcast_message message: "Scheduling your proofreading job..."
    proofreading_job.schedule
    broadcast_message message: "Redirecting to your quotation", path: proofreading_job_quotation_path(proofreading_job)
  end

  def broadcast_message(message:, head: 302, path: nil)
    ActionCable.server.broadcast "notification_channel_user_#{current_user_id}", head: head, path: path, content: message
  end

Вот мой тест Rspec:

RSpec.describe QuotationJob, type: :job do
  include ActiveJob::TestHelper

  let(:document)         { create(:document) }
  let(:current_user_id)  { document.proofreading_job.client_user.id }
  let(:proofreading_job) { document.proofreading_job }
  let!(:proofreader)     { create(:proofreader_with_work_events) }

  before do
    create(:job_rate, :proofreading)
  end

      it 'broadcasts messages to the current user while performing the job' do
        expect { QuotationJob.perform_now(document.id, proofreading_job.id, current_user_id) }
        .to have_broadcasted_to("notification_channel_user_#{current_user_id}").with(content: "Determining the exact amount of time to proofread your document")
      end

end

Вот результат теста:

  1) QuotationJob broadcasts messages to the current user while performing the job
     Failure/Error:
       expect { QuotationJob.perform_now(document.id, proofreading_job.id, current_user_id) }
       .to have_broadcasted_to("notification_channel_user_#{current_user_id}").with(content: "Determining the exact amount of time to proofread your document")

       expected to broadcast exactly 1 messages to notification_channel_user_23383 with {"content"=>"Determining the exact amount of time to proofread your document"}, but broadcast 0
       Broadcasted messages to notification_channel_user_23383:
         {"head":302,"path":null,"content":"Determining the exact amount of time to proofread your document"}
         {"head":302,"path":null,"content":"Determining the earliest delivery date..."}
         {"head":302,"path":null,"content":"Scheduling your proofreading job..."}
         {"head":302,"path":"/proofreading_jobs/6941/quotation","content":"Redirecting to your quotation"}

Я знаю, что это работает в процессе разработки, так как я вижу сообщения на экране. Но когда я запускаю тесты, сначала кажется, что сообщение не было отправлено. Но RSpec показывает мне, что на самом деле сообщения, которые я проверяю, были отправлены. Так как я могу спроектировать эти тесты, чтобы они работали?

...