Database records exists inside test file, but not in called functions

Hi

I have the following channel test case

setup do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Database.Repo)
    ...
    {:ok, %{admin_socket: admin_socket}}
end

 describe "REQUEST messages.create" do

    test "responds with a succesful payload", %{admin_socket: admin_socket} do
      conversation = Messenger.Factory.insert(:conversation)
      {:ok, _, admin_socket} = subscribe_and_join(admin_socket, "users:#{admin_socket.assigns.identity_id}", %{})

      payload = %{
        "method" => "messages.create",
        "context" => %{
          "id" => conversation.id
        },
        "data" => %{
          "type" => "message", "parts" => [%{"type" => "text", "body" => "hello"}]
          }}

      ref = push admin_socket, "request", payload

      assert_reply ref, :ok, %{context: %{type: "message.object"}}
      assert_broadcast "response", %{context: %{type: "message.object"}}
    end

  end

The odd thing is that if I Repo.all(Conversation) inside the above test case, it returns the factory-created conversation. However, inside the handle_in/3 callback in the channel, if I call Repo.all(Conversation), I get an empty list.

Any idea as to why?

1 Like