でモデルをテストする方法:エクト2.0を使用して、必要な団体
defmodule PlexServer.BoardInstanceTest do
use PlexServer.ModelCase
alias PlexServer.BoardInstance
@valid_attrs %{board_pieces: [%PlexServer.BoardTileInstance{x: 0, y: 0}], empire: %PlexServer.EmpireInstance{}}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = BoardInstance.changeset(%BoardInstance{}, @valid_attrs)
assert changeset.valid?
end
end
defmodule PlexServer.BoardInstance do
use PlexServer.Web, :model
alias PlexServer.BoardTileInstance
schema "board_instances" do
belongs_to :empire, PlexServer.EmpireInstance
has_many :board_pieces, BoardTileInstance
timestamps
end
@required_fields ~w()
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:board_pieces, required: true)
|> cast_assoc(:empire, require: true)
end
end
私のテストは
があるドキュメントを見てみると失敗しcast_assoc/3ニーズがキャスト/ 3の後に呼び出されることを言います私はこのテストがうまくいくためには何かが欠けていると確信しています。** (RuntimeError) casting assocs with cast/3 is not supported, use cast_assoc/3 instead
編集:私のコードを更新し、新しいエラーを受信:
** (Ecto.CastError) expected params to be a map, got:
%PlexServer.BoardTileInstance{__meta__: #Ecto.Schema.Metadata<:built>, fleets: #Ecto.Association.NotLoaded<association :fleets is not loaded>, id: nil, inserted_at: nil, system: #Ecto.Association.NotLoaded<association :system is not loaded>, updated_at: nil, x: 0, y: 0}
私は私の@valid_attrsを推測しているが、いくつかの方法が不正な形式?
に
を変更。 – Dogbert
あなたは@Dogbertの提案を変更しましたが、残念ながら私はまだ問題があります。 –
これらの2つの変更を行った後も同じエラーが発生していますか? – Dogbert