thedelchop

thedelchop

Using Wallaby and Mox with some nested LiveViews

Hello,

I’ve been using Wallaby lately and its been fantastic, so big thanks to the entire Wallaby team. In order to mock some 3rd party calls, I followed their docs and setup my own sandbox, so that, as I understand it, when I mock processes in the owner process (the test), those mocks can be shared with the child process (the actual live view process under test), and so far that has worked great.

Here’s a link to those docs, and my setup is identical to the example here with my own specific mocks substituted:

# config/support/sandbox.ex

defmodule YourApp.Sandbox do
  def allow(repo, owner_pid, child_pid) do
    # Delegate to the Ecto sandbox
    Ecto.Adapters.SQL.Sandbox.allow(repo, owner_pid, child_pid)

    # Add custom process-sharing configuration
    Mox.allow(MyMock, owner_pid, child_pid)
  end
end

However, I’ve recently nested a Live View within one of my live views, using live_render, and that breaks some of my tests. I am fairly confident that this is because the nested live view process hasn’t explicitly been granted access to the mock like the parent live view process has been.

I’m wondering if its possible to correct this issue without resorting to globally available mox, but sadly I don’t think so, because if I understand this correctly, when the Sandbox code is executed the child processes don’t even exist as of yet, so I have no pid which to allow.

Does anyone have any other strategies to handle this kind of problem, or is the best bet just running the test synchronously so that I can make the mock I define globally available.

First Post!

Neophen

Neophen

Did you have any luck fixing this?

Last Post!

jack456jokky

jack456jokky

Hello,

It’s great to hear it’s been working well for you. You’re right about the nesting of Live Views complicating the mock sharing. One approach you might consider is using a dynamic approach to grant permissions to nested Live Views.

You could leverage a :supervisor to manage the child processes and dynamically allow access to mocks during their initialization. This way, when you render the nested Live View, it can request access to the mocks from the parent’s context.

Alternatively, if it’s feasible, running your tests synchronously to ensure the mocks are globally accessible might simplify things. I’d be curious to hear how others are handling similar issues, especially with maintaining clean tests without relying on global mocks!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
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
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

We're in Beta

About us Mission Statement