Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"undefined function assigns/0" while trying to use Phoenix.LiveViewTest.rendered_to_string/1 #1798

Closed
maxmarcon opened this issue Dec 12, 2021 · 4 comments

Comments

@maxmarcon
Copy link
Contributor

Environment

  • Elixir version (elixir -v): 1.13
  • Phoenix version (mix deps): 1.6.4
  • Phoenix LiveView version (mix deps): 0.17.5
  • Operating system: MacOS 12.0.1

Actual behavior

Just trying out rendered_to_string (https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#rendered_to_string/1) in iex -S mix, using the very same example as in the docs (nevermind the typo in the function name in the docs):

import Phoenix.LiveView.Helpers
iex(2)> ~H"""                                         
...(2)> <div>example</div>                             
...(2)> """ |> Phoenix.LiveViewTest.rendered_to_string

Expected behavior

As in the docs: <div>example</div>

Actual behavior

warning: variable "assigns" does not exist and is being expanded to "assigns()", please use parentheses to remove the ambiguity or change the variable name
  iex:2

** (CompileError) iex:2: undefined function assigns/0 (there is no such import)
    (elixir 1.13.0) src/elixir_fn.erl:17: anonymous fn/4 in :elixir_fn.expand/4
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.13.0) src/elixir_fn.erl:23: :elixir_fn.expand/4
    (phoenix_live_view 0.17.5) expanding macro: Phoenix.LiveView.Helpers.sigil_H/2
    iex:2: (file)
    (elixir 1.13.0) expanding macro: Kernel.|>/2
    iex:4: (file)

I've actually discovered this while trying to use rendered_to_string in a live component test.

Is this a bug or am I forgetting something basic?

@josevalim
Copy link
Member

I will see if I can improve the error message here but you always need to have an assigns variable set before using the ~H sigilx

@maxmarcon
Copy link
Contributor Author

Thanks @josevalim. Yeah I'm aware that ~H needs assigns to be defined. However, I thought something might have changed when I came across these examples in the docs today on how to test function components: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#module-testing-function-components

import Phoenix.LiveView.Helpers
import Phoenix.LiveViewTest

test "greets" do
  assert rendered_to_string(~H"""
         <MyComponents.greet name="Mary" />
         """) ==
           "<div>Hello, Mary!</div>"
end

This wouldn't work, right? Something that might work instead would be:

import Phoenix.LiveView.Helpers
import Phoenix.LiveViewTest

test "greets" do
  assigns = []
  assert rendered_to_string(~H"""
         <MyComponents.greet name="Mary" />
         """) ==
           "<div>Hello, Mary!</div>"
end

So the docs need to be updated as far as I can see. I can come up with a little PR if you agree.

@josevalim
Copy link
Member

Please send a PR for the docs, totally!

@maxmarcon
Copy link
Contributor Author

PR is there

Zurga pushed a commit to Zurga/phoenix_live_view that referenced this issue Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants