How to Rspec system that ActionCable broadcast messages appear in the view












1















I want to test that messages that are broadcast when some background jobs are completed are actually appearing in the view.



I have unit tests for this which work fine. I would actually like to ensure that the JS gets run so that the view is updated with the correct message.



So far I have not been able to find any way to do this.



Here is the test I have where I would like to add the expectation for the broadcast message:



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


Here is my .coffee file:



$(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


Here is one of the background jobs that broadcasts a message when its done:



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


Any ideas on how to do this?










share|improve this question



























    1















    I want to test that messages that are broadcast when some background jobs are completed are actually appearing in the view.



    I have unit tests for this which work fine. I would actually like to ensure that the JS gets run so that the view is updated with the correct message.



    So far I have not been able to find any way to do this.



    Here is the test I have where I would like to add the expectation for the broadcast message:



    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


    Here is my .coffee file:



    $(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


    Here is one of the background jobs that broadcasts a message when its done:



    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


    Any ideas on how to do this?










    share|improve this question

























      1












      1








      1








      I want to test that messages that are broadcast when some background jobs are completed are actually appearing in the view.



      I have unit tests for this which work fine. I would actually like to ensure that the JS gets run so that the view is updated with the correct message.



      So far I have not been able to find any way to do this.



      Here is the test I have where I would like to add the expectation for the broadcast message:



      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


      Here is my .coffee file:



      $(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


      Here is one of the background jobs that broadcasts a message when its done:



      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


      Any ideas on how to do this?










      share|improve this question














      I want to test that messages that are broadcast when some background jobs are completed are actually appearing in the view.



      I have unit tests for this which work fine. I would actually like to ensure that the JS gets run so that the view is updated with the correct message.



      So far I have not been able to find any way to do this.



      Here is the test I have where I would like to add the expectation for the broadcast message:



      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


      Here is my .coffee file:



      $(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


      Here is one of the background jobs that broadcasts a message when its done:



      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


      Any ideas on how to do this?







      ruby-on-rails-5 rspec-rails actioncable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 4:37









      chellchell

      2,3671357110




      2,3671357110
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975319%2fhow-to-rspec-system-that-actioncable-broadcast-messages-appear-in-the-view%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975319%2fhow-to-rspec-system-that-actioncable-broadcast-messages-appear-in-the-view%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas