carlogilmar

carlogilmar

How you're testing Live Views in isolation?

Hello! Recently I’ve been working with the LiveViews, they are awesome, very easy, very simple, and very fast to build.

But what I not understanding at all is what is the best way to test a Live View, I know that most of the common tests are validating the HTML output just to see if a label is there or not. That’s nice. But I’d like to test the assigns map itself, my intuition tell me that could be great test the LiveView state, check the state when the LV is mounted, when there is a handle_params, when there is a handle_event, when there is a handle_info, etc…

Looking into this I tried to do something like check the process state and try to update it:

%{socket: %{assigns: assigns}} = :sys.get_state(view.pid)
      :sys.replace_state(view.pid, fn lv_view ->
        update_in(lv_view.socket.assigns, &Map.merge(&1, %{
          data: %{...}
        }))
      end)

This works, but I’m not convinced at all of implement this in my tests. Why I want this? Because sometimes a LV needs modules that are not ready, so I’d like to design in isolation just passing data.

Other option is explore a way to pass a function in the assigns and pass a function to mock data like:

assign_new(socket, :data, fn -> MyApp.get_data() end)
{:ok, view, _html} =
  live_isolated(conn, MyAppWeb.MyLive,
    assigns: %{data: %{...}}
  )

Maybe I’m not understanding something about how to test a LV. Maybe I’m just a crazy guys trying this ideas… but hey, maybe some of you have different approaches to do this.

Thanks for reading.

Most Liked

krasenyp

krasenyp

I really don’t see a reason to test the assigns as the rendered output should reflect the assigns.

Schultzer

Schultzer

This takes me back to early LV when the testing story left a lot to be decided. Today not so, use the element and render_* API, no need to fiddle with this. And if you’re broadcasting then you can start multiple live views so you can test how it would be to have multiple tabs open for a user or two different users!

egeersoz

egeersoz

Yeah, I see this mistake made often. Assigns should be considered an “implementation detail”, and testing them leads to a lot of brittleness. When it comes to testing one should test the end result, which in this case is the thing that gets rendered (or not).

eahanson

eahanson

I may not be understanding your comment, but I routinely inspect the HTML in tests to a much deeper degree than just looking at a label. I assume the examples of assert html =~ "Click me" are simplified to save space in the docs. I routinely assert on an HTML table’s full contents, assert on the enabled/disabled status of an entire page’s buttons, etc.

Where Next?

Popular in Questions Top

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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shahryarjb
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
baxterw3b
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
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
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement