faye-rails icon indicating copy to clipboard operation
faye-rails copied to clipboard

Testing Faye with RSpec

Open toomus opened this issue 9 years ago • 5 comments

Hi. Is it possible to test Faye with RSpec?

toomus avatar Mar 27 '15 10:03 toomus

OK, i know, that i have to use EventMachine, but now i have other problem. I want to test, that when I create new ChatMessage, it is published. When i subscribe to channel in my spec, messages published by model observer don't come. I wait for subscription and nothing happen. I put some prints to model observer and i see it is working.

EventMachine.run do
  client = Faye::Client.new('http://localhost:3000/faye')

  sub = client.subscribe("/chat/1") do |message|
    @message = message
    EventMachine.stop_event_loop
  end

  sub.callback do
    ChatMessage.create(content: "test message")
  end

  EventMachine.add_timer(4) do
    EventMachine.stop_event_loop
  end
end
expect(@message).not_to be_nil

What I am doing wrong? THX form help.

toomus avatar Apr 02 '15 08:04 toomus

OK, the problem is, although client subscribe channel, and receive success, it really doesn't subscribe. Server receives good message, but outgoing message looks like {"id"=>"4", "channel"=>"/chat/1", "successful"=>true}, so i think it mean, nobody is subscribing that channel. Am I right?

toomus avatar Apr 02 '15 11:04 toomus

I have the same issue as you, have you resolved it ?

mike927 avatar Aug 26 '15 09:08 mike927

Hi. The only way, to get this to work is to run your app before RSpec in 'test' env. You need also use DatabaseCleaner, and set in spec_helper.rb

   DatabaseCleaner.strategy = :deletion

to avoid problems with data visibility.

toomus avatar Aug 26 '15 10:08 toomus

Thanks for you reply then

mike927 avatar Aug 27 '15 07:08 mike927