coen.bakker
I wanted to test this server-side hook.
def on_mount(:default, _params, _session, socket) do
socket =
socket
|> assign(dialog_open: false)
|> assign(dialog_inner_block: nil)
|> assign(dialog_history: [])
|> attach_hook(:dialog_hook, :handle_event, &handle_event/3) # Causes error
{:cont, socket}
end
def handle_event("reset_dialog", _, socket) do
...
end
With this test.
setup %{conn: conn} do
%{conn: init_test_session(conn, %{})}
end
describe "on_mount: default" do
test "assigns dialog_open: false", %{conn: conn} do
session = get_session(conn)
{:cont, updated_socket} = Dialog.DialogHook.on_mount(:default, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.dialog_open == false
end
end
But I ran into this error.
1) test on_mount: default assigns dialog_open: false (MyAppWeb.DialogTest)
test/my_app_web/dialog_test.exs:26
** (KeyError) key :lifecycle not found in: %{__temp__: %{}}
code: {:cont, updated_socket} = Dialog.DialogHook.on_mount(:default, %{}, session, %LiveView.Socket{})
stacktrace:
:erlang.map_get(:lifecycle, %{__temp__: %{}})
(phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:92: Phoenix.LiveView.Lifecycle.lifecycle/2
(phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:45: Phoenix.LiveView.Lifecycle.attach_hook/4
(my_app 0.1.0) lib/my_app_web/dialog/dialog_hook.ex:11: MyAppWeb.Dialog.DialogHook.on_mount/4
test/my_app_web/dialog_test.exs:29: (test)
The socket.private doesn’t contain the key :lifecycle.
%LiveView.Socket{}
|> Map.get(:private)
|> IO.inspect()
=> %{__temp__: %{}}
I have been reading docs and searching around online, but cannot seem to find how to test this scenario.
How would I go about this? Any ideas?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
Other Trending Topics
A new screencast on live LiveView updates with PubSub, the Oban Web dashboard, and smarter retries. Part 2 of 2. Screencast + full walkth...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
I don’t think you can test those callbacks in isolation due to missing simple constructors for that state involved.
Instead I’d suggest creating a full LV module within the test and interact with that one to assert the hooks functionality.
coen.bakker
Let me see if I understand correctly.
So I create a minimal LiveView and attach to it the hook I want to test?
Would I be able to make assertions about the lv’s assigns directly? Since I see that the
lvreturned bylive_isolated/3does not carry the assigns.sodapopcan
With LiveView tests you can only assert on behaviour, ie, on the outputted HTML. Well, ok, you can use
:sys.get_stateto look at a process’ state, but I realllllly wouldn’t recommend it.Harking back to your recent post regarding testing, these are the types of unit tests I don’t bother with. Dialogs are very simple so there is really nothing to be gained from unit testing them in that way. So long as they open, display the correct contents, and all the buttons do what they are supposed to, then the desin of the internal state really doesn’t matter. It also means you could rejig the internals later on without changing the test. That is to say, a LiveViewTest asserting on HTML in a LiveView test is perfectly adequate here. Just my opinion, of course.
coen.bakker
Let’s say someone is stubborn and wants to see how they can test it anyway.
Would this be ridiculous?
Asking for a friend, of course.
coen.bakker
And I started reading the “Testing Elixir” book. Have good hope I’ll learn some valuable lessons from it. Want to demystify testing…
sodapopcan
For argument’s sake you could do this, yes, but you’re making a little more work for yourself, cluttering up your markup with test-related data, and coupling the test to the implementation.
LiveView tests are UI tests so we generally try and just test how the user interacts with it. A better option would be to set up a LiveView with a button that opens the dialog then you can write tests like this:
It’s up to your friend, of course!
coen.bakker
Maybe it’s my implementation then
.
The dialog root component is placed in a layout. All the live views that use that layout automatically can use that dialog. The live views will, however, pass the inner block that needs to be rendering inside the dialog dynamically.
So they never do this.
But instead they do.
Dailog.set_dialog(socket, &functional_component/1, payload). TheDialogmodule exposes all the dialog actions that are possible. There is alsoDialog.update_payload/1, for example.And the live views get events back from the dialog so they can react to an event appropriately according to their needs.
So far it works well, actually. So far.
Definitely learning from the process, so that’s a plus.
coen.bakker
Appreciate all the input!
sodapopcan
Sorry, that was me just not understanding how the dialog is set up so I just made it up, lol. The open button’s click event should be
<button phx-click="set_dialog/3">Open</button>and thenassert render(lv) =~ "test value". Of course maybe you aren’t using an open button? If not, just set up the LV however it’s going to work in the wild and then you can use all the functions inPhoenix.LiveViewTestto test it just like someone would actually interact with it.I do wonder why you aren’t using a LiveComponent for this, they are generally very well suited to dialogs! If you use a slot for the open trigger (again, if you even have one) you can keep all the state completely encapsulated in the component. And of course this style of testing makes it really easy to refactor to one.
No prob! I love talking testing. Though as I said in the other thread, I wouldn’t call myself an expert, so maybe I’ll say something stupid enough that it’ll cause someone else to jump in
I do know a thing or two, though 
coen.bakker
Well… That’s a great question.
Back when the dialog was just a baby it seemed unnecessary, but I think it’s time to reconsider
.
I was originally motivated by this “confirm” component in the LiveBook codebase.
https://github.com/livebook-dev/livebook/blob/main/lib/livebook_web/components/confirm.ex
I thought it was such a nice solution. But that was before I started stuffing whole inner_blocks and payloads down the dialog.