Created
October 24, 2017 10:52
-
-
Save ivanxuu/cc20e173a67ac22a7fea1c093c07bde7 to your computer and use it in GitHub Desktop.
context setup functions elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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