litaio / lita

ChatOps for Ruby.
https://www.lita.io
MIT License
1.68k stars 179 forks source link

Testing messages sent independently to a room using RSpec #172

Open samanthawardhaugh opened 8 years ago

samanthawardhaugh commented 8 years ago

I have written a handler for lita using the hipchat adapter. The handler posts an update to a set array of rooms stored in the handler's class every specified time period by iterating through the array and calling the following code

target = Lita::Source.new(user: Lita::User.find_by_name('user'), room: room_from_array, private_message: false) 
robot.send_message(target, msg)

The handler also has sleep and wake commands, which I am trying to test with RSpec, but I can't see whether it posts notifications while asleep, as the notifications are not a direct response to a user. Since RSpec can't access existing real rooms, I can't check that the rooms are receiving messages, and since the testing rooms are created anew every time, I can't add the testing room to my array of rooms to print to. Is there any way to access messages that are sent from a robot that aren't replies, or to add a room created in RSpec to the array I have stored?

jimmycuadra commented 8 years ago

I'm afraid the replies object in an RSpec example doesn't keep track of any data about the sent messages except for their text content. This is likely to be improved in the future. For now you can test this with a message expectation, even though it's not super nice looking. Something along these lines:

it "sends a message to the #foo room" do
  expect(robot).to receive(:send_message) do |target, message|
    expect(target.room.id).to eq("#foo")
  end
end

Although it's not part of your question, it's worth noting that in the example code you included, specifying the :user and :private_message arguments don't have any effect in this case and can be left out. If both a user and a room are supplied, the message is sent to the room when using robot.send_messages. The user is only used when a room is supplied if :private_message is true, or if you send the message using robot.send_messages_with_mention.