jimsynz / faye-rails

Simple Rails glue for the Faye messaging protocol.
MIT License
435 stars 79 forks source link

Testing Faye with RSpec #76

Open toomus opened 9 years ago

toomus commented 9 years ago

Hi. Is it possible to test Faye with RSpec?

toomus commented 9 years ago

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 commented 9 years ago

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?

mike927 commented 9 years ago

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

toomus commented 9 years ago

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.

mike927 commented 9 years ago

Thanks for you reply then