Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #120 from nickjer/send-msg-from-room
Browse files Browse the repository at this point in the history
allow user to send message from a room in tests
  • Loading branch information
jimmycuadra committed Jun 18, 2015
2 parents 5d7859c + b138ae6 commit 2b1ef35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/lita/rspec/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ def prepare_subject(base)
# Sends a message to the robot.
# @param body [String] The message to send.
# @param as [Lita::User] The user sending the message.
# @param as [Lita::Room] The room where the message is received from.
# @return [void]
def send_message(body, as: user)
message = if as == user
Message.new(robot, body, source)
else
Message.new(robot, body, Source.new(user: as))
end
def send_message(body, as: user, from: nil)
message = Message.new(robot, body, Source.new(user: as, room: from))

robot.receive(message)
end
Expand Down
28 changes: 28 additions & 0 deletions spec/lita/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

handler_class = Class.new(Lita::Handler) do
route(/^message$/, :message)
route(/^channel$/, :channel)
route(/^command$/, :command, command: true)
route("restricted", :restricted, restrict_to: :some_group)
route("admins only", :admins_only, restrict_to: :admins)
Expand All @@ -14,6 +15,15 @@ def message(response)
response.reply(response.user.name)
end

def channel(response)
if (room = response.message.source.room_object)
response.reply(room.id)
response.reply(room.name)
else
response.reply("No room")
end
end

def command(response)
response.reply("a", "command")
end
Expand All @@ -39,6 +49,12 @@ def self.name
it { is_expected.not_to route("message").to(:not_a_message) }
end

describe "routing channels" do
it { is_expected.to route("channel") }
it { is_expected.to route("channel").to(:channel) }
it { is_expected.not_to route("channel").to(:not_a_channel) }
end

describe "routing commands" do
it { is_expected.to route_command("command") }
it { is_expected.not_to route("command") }
Expand Down Expand Up @@ -94,6 +110,18 @@ def self.name
end
end

describe "#channel" do
it "replies with channel id if sent from room" do
room = Lita::Room.create_or_update(1, name: "Room")
send_message("channel", from: room)
expect(replies).to eq(%w(1 Room))
end
it "replies with no channel if not sent from room" do
send_message("channel")
expect(replies).to eq(["No room"])
end
end

describe "#command" do
it "replies with two strings" do
send_command("command")
Expand Down

0 comments on commit 2b1ef35

Please sign in to comment.