Skip to content

Instantly share code, notes, and snippets.

@ivanxuu
Created October 24, 2017 10:52
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 ivanxuu/cc20e173a67ac22a7fea1c093c07bde7 to your computer and use it in GitHub Desktop.
Save ivanxuu/cc20e173a67ac22a7fea1c093c07bde7 to your computer and use it in GitHub Desktop.
context setup functions elixir
defmodule HappoWeb.ContextHelpers do
@moduledoc """
# SETUP FUNCTIONS
#
# Example: setup [:fun_one, :fun_two]
#
# These functions help to prepare a test before execution. They
# receive a map with the context, returns `{:ok, new_context}`, and are
# chainable between them.
#
# ## Full example:
#
# describe "block" do
# setup [:fun_one, :fun_two]
# test "one plus two", %{one: one, two: two} do
# assert one + two = 3
# end
# end
# defp fun_one(ctx), do: {:ok, Map.put(ctx, :one, 1)}
# defp fun_two(ctx), do: {:ok, Map.put(ctx, :two, ctx.one + 1)}
"""
# Creates an user
def create_user(ctx) do
user = Factory.create!(:user)
{:ok, Map.put(ctx, :user, user)}
end
# Logs in the user present in context.
def log_user(%{conn: conn, user: user}=ctx) do
IO.puts inspect({user.email, user.password})
conn = post conn,
HappoWeb.Router.Helpers.session_path(conn, :create,
session: %{"email" => user.email, "password" => user.password})
# The logged user is now merged in conn.
{:ok, Map.put(ctx, :conn, conn)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment