Test the Subscriptions

Learn how to test your API and subscriptions.

We'll cover the following...

Subscriptions testing

Testing your API is important, and subscriptions are no exception. We’ve been using helpers from the PlateSlate.ConnCase module in our test to ease building HTTP based integration tests. We’ll need a similar PlateSlate.SubscriptionCase module to manage the subscription integration tests with channels. While the ConnCase module is generated by Phoenix when we first create the project, we’ll need to make the SubscriptionCase module ourselves:

Press + to interact
defmodule PlateSlateWeb.SubscriptionCase do
@moduledoc """
This module defines the test case to be used by
subscription tests
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with channels
use PlateSlateWeb.ChannelCase
use Absinthe.Phoenix.SubscriptionTest,
schema: PlateSlateWeb.Schema
setup do
PlateSlate.Seeds.run()
{:ok, socket} =
Phoenix.ChannelTest.connect(PlateSlateWeb.UserSocket, %{})
{:ok, socket} =
Absinthe.Phoenix.SubscriptionTest.join_absinthe(socket)
{:ok, socket: socket}
end
import unquote(__MODULE__), only: [menu_item: 1]
end
end
# handy function for grabbing a fixture
def menu_item(name) do
PlateSlate.Repo.get_by!(PlateSlate.Menu.Item, name: name)
end
end

This module sets up the socket we’ll use ...