(ArgumentError)参数错误-尝试将值列表传递给变更集时

寄件人

我正在尝试将值列表传递给变更集并具有以下方法:

模式和变更集方法:

defmodule Pubcus.Country do

    use Pubcus.Web, :model

  schema "countries" do

    field :code, :string
    field :en, :string
    field :de, :string
    field :it, :string
    field :fr, :string

    timestamps

  end

  def changeset_all(model, list) when is_list(list) do

        list
        |> Enum.map(fn param -> changeset(model, param) end)

  end


  def changeset(model, params \\ %{}) do

        model
    |> cast(params, [:code, :en, :de, :it, :fr])
    |> validate_required([:code, :en])
    |> validate_length(:code, max: 2)
    |> unique_constraint(:code)

  end

end

然后测试:

defmodule Pubcus.CountryModelTest do

    use Pubcus.ModelCase

    alias Pubcus.Country

    @valid_attrs %{ code: "CH", en: "Switzerland", de: "Schweiz", it: "Svizzera", fr: "Suisse" }
    @invalid_attrs %{ code: "CHH" }

    @valid_attrs_list [ %{code: "CH", en: "Switzerland", de: "Schweiz", it: "Svizzera", fr: "Suisse"},
                                          %{code: "IT", en: "Italy", de: "Italien"},
                                          %{code: "FR", en: "Franche", de: "Frankreich"} ]

    test "a valid dataset" do

      changeset = Country.changeset(%Country{}, @valid_attrs)
      assert changeset.valid?

    end

    test "a invalid country code" do

      errors = errors_on(%Country{}, @invalid_attrs)
      assert {:code, "should be at most 2 character(s)"} in errors
      assert {:en, "can't be blank"} in errors

    end

    test "a valid list of countries" do

      changeset = Country.changeset_all(%Country{}, @valid_attrs_list)
      assert changeset.valid?

    end


end

当我测试它时,它向我显示:

1) test a valid list of countries (Pubcus.CountryModelTest)
     test/models/country_model_test.exs:27
     ** (ArgumentError) argument error
     stacktrace:
       :erlang.apply([#Ecto.Changeset<action: nil, changes: %{code: "CH", de: "Schweiz", en: "Switzerland", fr: "Suisse", it: "Svizzera"}, errors: [], data: #Pubcus.Country<>, valid?: true>], :valid?, [])

我究竟做错了什么?

多伯特

问题是Country.changeset_all返回的s列表Ecto.Changeset,而不是一个Ecto.Changeset,所以您不能对此.valid?进行处理。您可以遍历列表并在每个列表上进行断言:

for changeset in Country.changeset_all(%Country{}, @valid_attrs_list) do
  assert changeset.valid?
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ArgumentError:升级 Rails 6.1.3 时出现错误数量的参数错误

变更集中的参数错误

序言列表参数错误

如何将参数传递给python函数错误

部署时WebService错误-参数错误

将动作委托作为参数传递给构造函数时出现参数错误

尝试打印数组参数错误

coffeescript传递require()参数错误

在结构列表上调用 std::sort 时将 const 作为“this”参数错误传递

尝试将数据作为BLOB插入,出现参数错误

即使参数正确传递,参数错误也太少

Oracle PL / SQL:传递参数时发生函数错误

Swift将数组作为参数错误传递

代码中的参数正确,编译时参数错误

附加到列表时出现 Aerospike 参数错误

使用addWeighted()时,OpenCV引发错误的参数错误

调用过程时错误的数量或类型的参数错误

将函数作为参数传递给另一个函数错误

GridsearchCV:尝试在参数中传递lambda时,无法腌制函数错误

Ocelot 缓存:AddCacheManager 尝试明确指定类型参数错误

尝试返回类类型,但模板参数错误

在构造函数错误中将类指针作为参数传递

尝试在R中设置哈希图的值时,环境子分配的参数错误

PHP / PDO动态绑定值(无效的参数计数错误)

使用org-agenda时的类型参数错误

创建Tibble表时未使用的参数错误

在Rails中编码视频时出现参数错误

使用内在函数时如何避免“ out”参数错误?

Elixir:从 Elixir 任务访问 ETS 时出现参数错误