evancloutier

evancloutier

Dynamically apply method from Phoenix.ConnTest

I’m in the process of creating some reusable test modules using a macro:

defmodule SharedTestCase do
  defmacro define_tests(do: block) do
    quote do
      defmacro __using__(options) do
        block = unquote(Macro.escape(block))

        quote do
          use ExUnit.Case

          @moduletag unquote(options)
          unquote(block)
        end
      end
    end
  end
end

I’ve gotten as far as defining a reusable method for post/3 requests, but would like to extend to supply the verb as a method. I’m unsure how to invoke the methods supplied by Phoenix.ConnTest dynamically as Kernel.apply/3 expects a module as the first argument. Here is a working example for post requests:

defmodule EnsureAuthenticatedConnTests do
  import SharedTestCase

  define_tests do
    test "returns an error when the connection has no form of authentication", %{data: data, route: route} do
      assert %{
        "errors" => [%{
          "message" => "Unauthorized"
        }]
      } == build_conn()
      |> put_req_header("accept", "application/json")
      |> post(route, data)
      |> json_response(401)
    end
  end
end

Ideally, post becomes a method invoked by Kernel.apply/3. Using the module:

defmodule MyApp.SomeControllerTest do
  use MyApp.ConnCase

  use EnsureAuthenticatedConnTests, data: %{"foo" => "bar}, route: "/users"
end

Any thoughts would be appreciated!

Most Liked

dimitarvp

dimitarvp

Felt so bad that I posted in the wrong thread here so I came back: OP, did you solve your problem?

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement