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
Felt so bad that I posted in the wrong thread here so I came back: OP, did you solve your problem?
1
Popular in Questions
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
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
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
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
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
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
Hello, how can I check the Phoenix version ?
Thanks !
New
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








