antonmi / espec_phoenix

ESpec for Phoenix web framework.
MIT License
138 stars 33 forks source link

What about LiveView? #60

Closed Bastes closed 2 years ago

Bastes commented 3 years ago

Hi,

My team's considering espec to build a new phoenix project, one that will use liveview heavily and therefore we would like to test those liveviews.

Searching on how to do the plumbing between espec and phoenix, I only found this project, which installs nicely, but I can't find documentation about testing liveviews in your README.md, and when I try to just translate a simple exunit live view test into what I assume should be the way to to those with your package, here's what I get:

defmodule MyApp.PageLiveSpec do
  use ESpec.Phoenix, controller: MyApp.PageLive, async: false

  describe "GET /" do
    it "displays the page" do
      {:ok, page_live, disconnected_html} = live(build_conn, "/")
      assert disconnected_html =~ "My App's Title"
      assert render(page_live) =~ "My App's Title"
    end
  end
end

# testing:
# == Compilation error in file spec/my_app/live/page_live_spec.exs ==
# ** (CompileError) spec/my_app/live/page_live_spec.exs:7: undefined function live/2

So... I'm kind of wondering if there is live_view support in this package :sweat_smile: ; is there? If there is, what am I missing? If not, will there be sometimes in the future?

Thanks in advance :)

Bastes commented 3 years ago

Ok, tried importing Phoenix.LiveViewTest hoping it solves the problem, with:

defmodule MyApp.PageLiveSpec do
  use ESpec.Phoenix, controller: MyApp.PageLive, async: false

  import Phoenix.LiveViewTest

  describe "GET /" do
    it "displays the page" do
      {:ok, page_live, disconnected_html} = live(build_conn, "/")
      assert disconnected_html =~ "My App's Title"
      assert render(page_live) =~ "My App's Title"
    end
  end
end

Got:

    spec/my_app/live/page_live_spec.exs:7
    ** (ArgumentError) fetch_test_supervisor!/0 can only be invoked from the test process
          (phoenix_live_view 0.15.7) lib/phoenix_live_view/test/live_view_test.ex:359: Phoenix.LiveViewTest.fetch_test_supervisor!/0
          (phoenix_live_view 0.15.7) lib/phoenix_live_view/test/live_view_test.ex:301: Phoenix.LiveViewTest.connect_from_static_token/2
          spec/my_app/live/page_live_spec.exs:9: MyApp.PageLiveSpec.example_displays_the_page_cjkvbieutrfosynqzmxghldwap/1
          (espec 1.8.3) lib/espec/example_runner.ex:86: ESpec.ExampleRunner.try_run/3
          (espec 1.8.3) lib/espec/example_runner.ex:56: ESpec.ExampleRunner.run_example/2
          (elixir 1.11.3) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
          (elixir 1.11.3) lib/task/supervised.ex:35: Task.Supervised.reply/5
          (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

So, no luck just using the original function :/

karlosmid commented 2 years ago

https://github.com/antonmi/espec_phoenix/commit/18960a9b47b79fd1ebbbac182f7462715595f08f