JonRowe

JonRowe

Better named test helper for building conn for unit tests

Moved from Feature suggestion: Improved test helper for building a conn for unit test's · Issue #3640 · phoenixframework/phoenix · GitHub.

The test helpers for creating a conn suitable for unit testing controller helpers and plugs are named in such a way it’s hard to find them. Even as a relatively experienced Phoenix developer bypass_through means nothing to me and I struggled to find it, and even then struggled to use it correctly.

Before I found it I tried:

  • build_conn()
  • build_conn() |> fetch_flash()
  • build_conn() |> fetch_session() |> fetch_flash()

Before reaching to slack where I was informed of the bypass_through helper and reaching for:

  • build_conn() |> bypass_through()
  • build_conn() |> bypass_through() |> fetch_flash()
  • build_conn() |> bypass_through() |> fetch_session() |> fetch_flash()

Etc etc you get the idea, what finally worked for me is:

build_conn() |> bypass_through(AppWeb.Router, [:pipeline]) |> get("/")

This is pretty ridiculous when all you want to do is assert a module puts a flash or redirects, in my case I was building a set of extracting authentication helpers to allow me to dry up my controllers. bypass_through is completely “non glanceable” when looking to solve this problem.

Ways to improve

My preferred solution for fixing this is to introduce build_conn_with_pipeline/2 that takes the router and the pipeline, its name will mean its at least findable when looking for this feature set, and can quite happily be built from and point to the more composable existing functions.

An alternate solution is to rename/alias bypass_through as something more obvious, run_pipeline maybe? (Although I’d make it not need the get in that scenario).

A final “least effort” solution, would be to improve the the build_conn documentation and/or error message presented by test helpers operating on a raw build_conn to highlight the need to run bypass_through when doing any kind of non endpoint conn testing…

As per the Github issue I’m happy to open a PR for any of these but I can’t afford to waste my limited open source time building something that that’d be rejected, so opening for discussion first.

First Post!

shanesveller

shanesveller

If the original. deficiency is in the docs, we should start by soliciting and contributing improvements there before introducing another technical abstraction on top of the existing concepts, that may or may not be more accessible to various readers.

Where Next?

Popular in Discussions Top

sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
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
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
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
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement