vendredi 20 mai 2016

elixir ecto: test has_many association

I am a newbie in elixir, so don't be too hash I have following models:

defmodule MyApp.Device do
  use MyApp.Web, :model

  schema "devices" do
    field :name, :string
    belongs_to :user, Typi.User

    timestamps
  end
end

defmodule MyApp.User do
  use MyApp.Web, :model

  schema "users" do
    field :name, :string
    has_many :devices, Typi.Device

    timestamps
  end
end

How do I test that user has many devices in an elegant way. For instance

  test "registration generates user with devices" do
    changeset = Registration.changeset(%Registration{}, @valid_attrs)
    registration = Ecto.Changeset.apply_changes(changeset)
    user = Registration.to_user(changeset)
    IO.puts "#{inspect user}"
    # assert device is inside the user
  end

Aucun commentaire:

Enregistrer un commentaire