polypush135

polypush135

ExUnit anti pattern?

I wanted to know if this is a good patter for setting up a context in ExUnit with phoenix?

This test is using ConnCase so from the looks of it, the ConnCase setup runs first and then is followed by the setup inside the test file.

Thoughts on how to improve this pattern?

defmodule Opalnova.PostControllerTest do
  import Opalnova.Factory #note using ex_machina via insert(:post)
  use Opalnova.ConnCase

  ...
  
  setup context do
    case context do
      %{with_post: true} ->
        {:ok, %{conn: context.conn, post: insert(:post) }}
      _ ->
        :ok
    end
  end

  test "lists all entries on index", %{conn: conn} do
    conn = get conn, post_path(conn, :index)
    assert html_response(conn, 200) =~ "Listing posts"
  end

  ...

  @tag :with_post
  test "renders form for editing chosen resource", %{conn: conn, post: post} do
    conn = get conn, post_path(conn, :edit, post)
    assert html_response(conn, 200) =~ "Edit post"
  end

end

the {:ok, %{conn: context.conn, post: insert(:post) }} feels dirty

Most Liked Responses

karmajunkie

karmajunkie

I don’t think there’s anything inherently wrong with it—its an interesting technique I haven’t seen before, actually, and I can see that it might be useful sometimes. But in this case, I think you’re better off just duplicating the insert(:post) in the tests that need it rather than being clever with tags. If you find that the duplication gets more complex, i would probably split my test into two different files where each test in the file uses the same setup scenario. The cognitive load in connecting the tag with your setup is kind of high for what I see as a small savings.

Just my two cents, YMMV…

karmajunkie

karmajunkie

Yeah, coming from a heavy Ruby background myself I can see where you were coming from. I think you’ll probably find that in the general consensus in Elixir is that rspecs let and nested contexts do more to detract from maintainability than they help, so they’ve been left out by intention.

What you might try instead of tag-based setup is to collect repetitive setup steps into a set of functions you can call to quickly init a given scenario, leaning less on the setup context. Those can also be moved into an imported module for use across tests if needed.

polypush135

polypush135

I agree with your assessment and maybe my example is contrived.

To give you a little more back ground, I’m coming from ruby + rspec and I was looking for something comparable to rspec’s let + context block. I wanted to find a way where setup could be identifiable as only useful in a given context.

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement