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

Channel reset after spec test not working #46

Closed patmbolger closed 8 years ago

patmbolger commented 8 years ago

I am using V 1.5 of the gem. I have a couple of simple respect tests that confirm that a channel is being opened when an event occurs on the server side of my app.

Here is the test code:

require 'rails_helper'

RSpec.describe Event, type: :model do

  after(:each) do
    PusherFake::Channel.reset
  end

  describe 'company_registered event' do
    it 'establishes pusher_control channel' do
      expect{ Event.create(:COMP_REGISTER, name: 'Widgets, Inc.', id: 1) }.
            to change{ Pusher.channels[:channels].count }.by(+1)
    end
  end

  describe 'js_registered event' do
    it 'establishes pusher_control channel' do
      expect(Pusher.channels[:channels].count).to eq 0
      # expect{ Event.create(:JS_REGISTER, name: 'Sam Smith', id: 1) }.
      #       to change{ Pusher.channels[:channels].count }.by(+1)
    end
  end
end

Here is the code under test:

class Event
  include ActiveModel::Model

  def self.create(evt_type, evt_data)
    case evt_type
    when :JS_REGISTER     # job seeker registered
      Pusher.trigger('pusher_control', 'js_registered', evt_data)
    when :COMP_REGISTER   # company registration request
      Pusher.trigger('pusher_control', 'company_registered', evt_data)
    end
  end
end

I get this output when I run the tests:

Event
  company_registered event
    establishes pusher_control channel
  js_registered event
    establishes pusher_control channel (FAILED - 1)

Failures:

  1) Event js_registered event establishes pusher_control channel
     Failure/Error: expect(Pusher.channels[:channels].count).to eq 0

       expected: 0
            got: 1

The first test always succeeds and the second fails, regardless of which order I run the tests. So, somehow the channel reset is not actually working.

I have this in spec/rails_helper.rb:

require 'pusher-fake/support/rspec'

And I am initializing the Pusher app ID, key and secret in config/initializers/pusher.rb.

Note that I running these tests as model rspec tests. Might that cause PusherFake::Channel.reset not to work? If so, any idea on a work-around? (for various reasons I am creating Pusher events from a model, not a controller).

Thanks for your help.

tristandunn commented 8 years ago

You're publishing events on the same channel. See Publishing Events.

tristandunn commented 8 years ago

Sorry, I totally read the issue incorrectly. Looking into it.

tristandunn commented 8 years ago

You're checking Pusher.channels not PusherFake::Channel.channels.

Also, I'm not sure a test like this is going to work since the servers are in a fork. It's not design to be for unit testing like this. And a unit test isn't typically supposed to check the integration so much. Why not just verify the Pusher.trigger method calls and verify the full workflow in an integration test?

patmbolger commented 8 years ago

You're right and I realize I was doing something here not entirely suited to the design (limited workflow testing from a model (unit) rspec test). I thought I'd give it a shot in any case. I will take your advice and focus on acceptance/cucumber tests for the complete workflow.

Thanks for taking a look for me.