Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/0ff25b5cccc290ecf9f5 to your computer and use it in GitHub Desktop.
Save paulcsmith/0ff25b5cccc290ecf9f5 to your computer and use it in GitHub Desktop.
defmodule AnnouncementChannelTest do
use ConstableApi.TestWithEcto, async: false
alias Phoenix.Socket
alias Phoenix.Socket.Message
alias ConstableApi.Repo
alias ConstableApi.Announcement
alias ConstableApi.AnnouncementChannel
test "announcements:index returns announcements with ids as the key" do
announcement = %Announcement{title: "Foo", body: "Bar"} |> Repo.insert
announcement_id = to_string(announcement.id)
handle_in_topic(AnnouncementChannel, "announcements:index")
announcements = %{} |> Map.put(announcement_id, announcement)
assert_socket_replied_with_payload("announcements:index", announcements)
end
test "announcements:create returns an announcement" do
params = %{"title" => "Foo", "body" => "Bar"}
Phoenix.PubSub.subscribe(self, "announcements:create")
handle_in_topic(AnnouncementChannel, "announcements:create", params)
announcement = Repo.one(Announcement)
assert_socket_broadcasted_with_payload("announcements:create", announcement)
end
def assert_socket_broadcasted_with_payload(topic, payload) do
assert_received {:socket_broadcast,
%Message{event: topic, payload: payload, topic: topic}
}
end
def assert_socket_replied_with_payload(topic, payload) do
assert_received {:socket_reply,
%Message{event: topic, payload: payload, topic: topic}
}
end
def handle_in_topic(channel, topic, params \\ %{}) do
socket = Socket.put_current_topic(new_socket(topic), topic)
channel.handle_in(topic, params, socket)
socket
end
def new_socket(topic) do
%Socket{
pid: self,
router: ConstableApi.Router,
topic: topic,
assigns: []
}
end
end
@paulcsmith
Copy link
Author

A first pass at testing Phoenix Channels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment