williamthewill

williamthewill

Generating separate tests with for - how to make a controller test to validate mandatory fields?

Hi everyone!
I need make a controller tests to validate mandatory fields. In this case the tests is the same for all fieds and the validation too.
Use a loop to generate the same test for different values is wrong?

Enum.each(["document_number", "name", "percentage_share"], fn attribute ->
  @attribute attribute
  @tag new_setup: true
  test "returns error to #{@attribute} when send subject financial account authorization request",
      %{
        partner_conn: partner_conn,
        subject_cnpj_attrs: subject_cnpj_attrs
      } do

    subject_cnpj_attrs =
      Map.merge(subject_cnpj_attrs, %{
      "company_name" => "teste",
      "social_name" => "teste",
      "creation_date" => "2015-04-01",
      "address_street" => "rua antonio pontes filho",
      "address_number" => "310",
      "address_postal_code" => "16201220",
      "address_city" => "santana",
      "address_state" => "FP",
      "address_country" => "montreal",
      "net_monthly_average_income" => 4000,
    })

    documents = [
      %{
        "file_type" => "application/pdf",
        "file_url" => "social-contract-url",
        "labels" => ["social-contract"]
      }
    ]

    partners = [ %{"address_country" => "franca"} ]

    [partner_1, partner_2] = partners
    partner_1 = Map.delete(partner_1, @attribute)

    subject_cnpj_attrs =
      Map.merge(subject_cnpj_attrs, %{"documents" => documents})
      |> Map.merge(%{"partners" => [partner_1, partner_2]})

    subject =
      partner_conn
      |> create_company(subject_cnpj_attrs)
      |> json_response(422)
      |> Map.get("errors")

    assert %{"partners" => [%{"#{@attribute}" => ["can't be blank"]}, %{}]} == subject
  end
end)

Most Liked

tommasoamici

tommasoamici

That should work. You’ll want to unquote(attribute) in the body of the test though.

This article helped me when writing tests this way: 🐥 Parameterized tests in Elixir ExUnit | 🐥 YellowDuck.be

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement