I have two module, book and word, and following are the schema.
schema "books" do
field :name, :string
many_to_many :words, Hello.Contents.Word, join_through: "books_words", on_delete: :delete_all
timestamps()
end
schema "words" do
field :name, :string
many_to_many :books, Hello.Contents.Book, join_through: "books_words", on_delete: :delete_all
timestamps()
end
I write a test case.
test "many_to_many :words" do
book = book_fixture()
word = word_fixture()
book = Repo.preload(book, [:words])
book_changeset = Ecto.Changeset.change(book)
book_words_changeset = book_changeset |> Ecto.Changeset.put_assoc(:words, [word])
Repo.update!(book_words_changeset)
assert book.words == [word]
end
The test case result.
What is the right way to test many_to_many association?