tristandunn / pusher-fake

A fake Pusher server for development and testing.
https://rubygems.org/gems/pusher-fake
MIT License
173 stars 32 forks source link

Can't seem to get PusherFake working in tests #33

Closed joemsak closed 9 years ago

joemsak commented 10 years ago

Hey, I am not sure what I'm doing wrong, but pusher-fake isn't working in tests, or in development in the browser for me. Also, if I follow the part in the documentation that says NOT to escape the javascript method with raw/html_safe, it totally doesn't work, just printing out html entities.

# Gemfile
group :test, :development do
  gem 'pusher-fake', '~> 1.2.0'
end
# config/initializers/pusher.rb
Pusher.app_id = ENV['PUSHER_APP_ID']
Pusher.key = ENV['PUSHER_KEY']
Pusher.secret = ENV['PUSHER_SECRET']
Pusher.logger = Rails.logger
unless Rails.env.production?
  require 'pusher-fake/support/base'
end
# spec/rails_helper.rb
require 'pusher-fake/support/rspec'
# the html page where Pusher JS client is used
<script>
  $(function() {
    <% pusher_options = { authEndpoint: '/messenger/auth' } %>

    <% unless Rails.env.production? %>
      Pusher.log = function(message) {
        if (window.console && window.console.log) {
          window.console.log(message);
        }
      };
    <% end %>

    var pusher = <% if defined?(PusherFake) %>
                   <%= PusherFake.javascript(pusher_options).html_safe %>
                 <% else %>
                   new Pusher("<%= ENV['PUSHER_KEY'] %>", <%= pusher_options.to_json.html_safe %>)
                 <% end %>,
        channel = pusher.subscribe('<%= Messenger.channel_id(@group_session) %>');

    channel.bind('session_is_live', function(data) {
      console.log(data);
      var $link = $('<a></a>')
              .text("<%= t('text.models.group_session.join_live_session_now') %>")
              .attr('href', data.url),
          $youtubeLink = $('<a></a>')
                   .text("<%= t('text.models.group_session.watch_on_air_now') %>")
                   .attr('href', "http:/youtube.com/watch?v=" + data.youtubeId);

      if (data.url !== undefined) {
        $('#join_session').html($link);
      }

      if (data.youtubeId !== undefined) {
        $('#watch_live').html($youtubeLink);
      }
    });
  });
</script>
# feature spec
  scenario 'see the link to join on the page' do
    user = create(:signup)
    group_session = create(:group_session)
    page = GroupSessionPage.new(group_session)

    Booking.create(group_session, user)
    logged_in(user) { page.visit }

    Messenger.notify(group_session, 'session_is_live', { url: 'http://url' })

    expect(page).to have_link(page.join_group_session_link_text, href: 'http://url')
  end

# The expectation fails, the link is never generated. Pusher is working fine in the browser, but PusherFake doesn't work in tests or the browser.
# Messenger

require 'pusher'

class Messenger
  class << self
    def notify(client, event, payload)
      @client = client
      Pusher.trigger(channel_name, event, payload)
    end

    def authenticate(channel, socket)
      Pusher[channel].authenticate(socket)
    end

    def channel_id(client)
      @client = client
      channel_name
    end

    private
    attr_accessor :client

    def channel_name
      "private-#{client.id}"
    end
  end
end

I tried switching to selenium/firefox to pause the tests and check the console, and it all seems to be logging and connecting fine for the channel and the subscription, but the binding doesn't seem to be working. When I send the Messenger.notify in the test or in rails console it doesn't log that event being received

When I use normal pusher in development, everything works perfectly

joemsak commented 10 years ago

Also, sorry I forgot to say: Rails 4.1.6, ruby 2.1.2

tristandunn commented 10 years ago

Any luck on a solution? I don't see anything immediately wrong.

Have you tried enabling verbose logging for PusherFake? That should help confirm if it's sending the event. If not then it might be worth digging into the Thin server to confirm it's receiving it there and to see where it's failing. (I should add logging to it too.)

tristandunn commented 9 years ago

Going to assume you worked this out, so I'm closing. Feel free to reopen if that's not the case.