Hi,
I’ve created a simple Phoenix channel client using phoenix_gen_socket_client
(GitHub link) for a Raspberry Pi that controls doors.
The client works well and as expected, but I’m having trouble working out the best way of going about writing unit tests to check the client works as expected. I’ve looked in the documentation and found Phoenix.Channels.GenSocketClient.TestSocket
but can’t work out how to implement it or if it actually does what I require.
The code for my client implentation looks something like this:
def start_link(opts) do
name = Keyword.get(opts, :name, __MODULE__)
GenSocketClient.start_link(
__MODULE__,
Phoenix.Channels.GenSocketClient.Transport.WebSocketClient,
opts, # arbitary argument
[], # socket opts
name: name # GenServer opts
)
end
def init(_opts) do
{url, name} = get_socket_opts()
{:connect, url, [{"name", name}], %{first_join: true, ping_ref: 1}}
end
def handle_message(_topic, "mode:pair", payload, _transport, state) do
store = SmartHomeFirmware.State.get(:lock)
|> Map.replace!(:mode, @modes.pair)
SmartHomeFirmware.State.put(:lock, store)
SmartHomeFirmware.State.put(:pair_params, payload)
{:ok, state}
end
def handle_info(:reset_state, transport, state) do
{:ok, ref} = GenSocketClient.push(transport, state.channel, "reset", %{})
{:ok, Map.put(state, :reset_reply, ref)}
end
The whole file is on GitHub here: https://github.com/dwyl/smart-home-firmware/blob/master/lib/smart_home_firmware/client/hub_client.ex
I want to be able to call the handler functions and check they do what they’re intended to do. I know I can manually call some of the functions, e.g. handle_message
, but how would I go about checking the messages pushed to the socket?
Is there a way to use a transport
that allows me to assert the content of the pushed message in a unit test?
Thanks for your time and help.