Edit the fields of an Ecto record in a destructuring for a test

I’m currently creating a test to test a LiveView phx-click event. For the above, I have the following setup for the tests:

setup do
      %{id: follower_id, show: %{slug: slug, id: show_id} = show} = insert(:follower)
      %{id: follower_id, slug: slug, show: show}
end

follower is created through a factory that has a show associated with it that is also created through a factory. But this creation of the show does not assign any value to the status field of the follower.

The structure of a show record is similar to the following (the status field can be active or discarded)

%Database.Show{
    ...
    status: :active,
    ...
}

How can I change or assign a value to the status field of the show in the test setup (not in the show factory)?

Can you show the inspect output for
insert(:follower)
?

%Database.Follower{
  ...
  email: "a1s2d33d4f40@testmail.com",
  ...
  show: %Database.Show{
    ...
    status: :discarded,
    ...
  }
  ...
}

I omit some fields and information on purpose.

I’m not sure I follow what you want to do here. Can you paste a full fuctional code fragment and explain what you expect to happen? I think there is some info missing, e.g. the setup callback should return a keyword list or :ok atom.

I need to change the state of the field to active in the test setup, to be able to do my tests correctly.

Expected:

%Database.Follower{
  ...
  email: "a1s2d33d4f40@testmail.com",
  ...
  show: %Database.Show{
    ...
    status: :active,
    ...
  }
  ...
}

Do you use ExMachina for factories? If so you should be able to implement something similar to:

show = insert(:show, status: :active)
follower = insert(:follower, show: show)

Or you could just update the nested status field directly:

follower =
  insert(:follower) 
  |> put_in([:show, :status], :active)

Does that help?

1 Like