Phoenix LiveView测试无法找到字段

问题描述

早上好

一段时间不使用Phoenix后,我尝试退出Phoenix LiveView,并使用内置的生成器创建LiveView资源Wiki.Page。其中一项测试如下所示:

@create_attrs %{body: "some body",subject: "some subject",tags: []}
@update_attrs %{body: "some updated body",subject: "some updated subject",tags: []}

test "saves new page",%{conn: conn} do
  {:ok,index_live,_html} = live(conn,Routes.wiki_page_index_path(conn,:index))

  assert index_live |> element("a","New Page") |> render_click() =~
           "New Page"

  assert_patch(index_live,:new))

  assert index_live
         |> form("#page-form",page: @invalid_attrs)
         |> render_change() =~ "can't be blank"

  {:ok,_,html} =
    index_live
    |> form("#page-form",page: @create_attrs)
    |> render_submit()
    |> follow_redirect(conn,:index))

  assert html =~ "Page created successfully"
  assert html =~ "some body"
end

记录本身的模式如下:

schema "wiki_pages" do
  field :body,:string
  field :subject,:string
  field :tags,{:array,:string}
  belongs_to :parent,__MODULE__
  timestamps()
end

由于tags是一个数组,因此标签选择的名称将类似于page[tags][],但是测试一直在寻找名为page[tags]的字段。每当我运行测试时,都会出现以下错误:

1) test Index saves new page (IronWeb.Wiki.PageLiveTest)
   test/iron_web/live/wiki/page_live_test.exs:34
   ** (ArgumentError) could not find non-disabled input,select or textarea with name "page[tags]" within:
 
       <input name="_csrf_token" type="hidden" value="KHJzRw4MU0B-N1lkH1lZEgcCNjo3HCsNOJFuwoax4e-7p6aYORCvtCBa"/>
       <input id="page-form_subject" name="page[subject]" type="text"/>
       <textarea id="page-form_body" name="page[body]">
      </textarea>
       <select id="page-form_tags" multiple="" name="page[tags][]"><option value="option1">Option 1</option><option value="option2">Option 2</option></select>

 
   code: |> render_change() =~ "can&apos;t be blank"
   stacktrace:
     (phoenix_live_view 0.14.8) lib/phoenix_live_view/test/live_view_test.ex:876: Phoenix.LiveViewTest.call/2
     test/iron_web/live/wiki/page_live_test.exs:44: (test)

我为此进行了谷歌搜索,但是我想不出一种方法来告诉它表单字段的末尾应带有方括号。任何援助将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)