Search code examples
elixirectoex-unit

How to test models with required associations


Using Ecto 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

My test fails with

** (RuntimeError) casting assocs with cast/3 is not supported, use cast_assoc/3 instead

Looking at the documentation is says that cast_assoc/3 needs to be called after cast/3 so I'm pretty sure I'm missing something essential to get this test to work.

Edit: Updated my code and now receiving a new error:

** (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}

I'm guessing my @valid_attrs are malformed some how?


Solution

    1. You don't need to pass the names of associations to cast or validate_required. You should remove it from @required_fields. cast_assoc will handle converting those fields to structs and, if you pass required: true, will validate that they're present. (For those who didn't read the comments above, see revision 1 of the question for the context.)

    2. @valid_attrs should be a normal map, like you would get as params in a Phoenix Controller's functions. cast_assoc will handle converting a raw map into a struct. So, change

      @valid_attrs %{board_pieces: [%PlexServer.BoardTileInstance{x: 0, y: 0}], empire: %PlexServer.EmpireInstance{}}
      

      to

      @valid_attrs %{board_pieces: [%{x: 0, y: 0}], empire: %{}}