Moussenger

Moussenger

Hi!

I’m trying to avoid redundant testing (that means, tests that are made in the same way all the time but with slightly different parameters). For example, I have this all the time when I want to test that the call to a controller was ok:

setup do
  count = 3
end

test "responds with right schema", %{conn: conn, swagger_schema: schema} do
    conn
    |> create_authenticated_conn()
    |> get(~p"/something",  %{a: 1})
    |> validate_resp_schema(schema, "Schema")
    |> json_response(200)
    |> assert_length(count)
end

The things that change are the method call (get, post…), the params, the schema to evaluate, and, optionally if I want to make more assertions (as checking the length of the result array).

So, I thought of creating a macro that simplifies all this work, parameterizing the values that need to be parameterized and also allowing a do: block element to add additional checks. I end up with this:

defmacro test_ok(schema_name, method, url, params \\ nil, do: block) do
  quote do
    test "responds with right schema", %{conn: conn, swagger_schema: schema} do
      response =
        conn
        |> create_authenticated_conn()
        |> unquote(method)(unquote(url), unquote(params))
        |> validate_resp_schema(schema, unquote(schema_name))
        |> json_response(200)

      evaluate_response(response, block)
    end

    defp evaluate_response(var!(response), block) do
      unquote(block)
    end
  end
end

If I ignore the response part and the block part (evaluate_response method) all works fine. As soon as I want to add to the macro the block part and pass the response to inside the body, I have two problems. First, let’s put an example of code:

test_ok("Schema", :get, ~p"/something") do
  assert_length(response, count)
end

Then, the first problem is about the response not being recognized inside the block code. And the other is count not being available neither, although in the way normal test macro, is recognized perfectly.

What is my mistake here?

Showing Posts 1 to 1

ziinc

ziinc

You can use a for..do loop in an exunit test. I highly recommend not over-complicating your tests with a macro unless you absolutely need to.

See this for an example

To answer your question, your block is a compile time ast and can’t be used like an arg at runtime.

Also, you’d be redefining a new private function with the same arity for each macro, so it would not work for more than 1 test.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews