Jaffa
Jaffa

Reputation: 12719

doctest a phoenix context

I'm new to elixir and phoenix and I wanted to doctest a newly created context (using mix phx.gen.context).

I have an Accounts context with a User schema.

I added to accounts_test.exs the following lines:

alias MyApp.Accounts.User
doctest MyApp.Accounts.User, import: true

And even fixed the default change_user documentation:

-     %Ecto.Changeset{source: %User{}}
+     %Ecto.Changeset{data: %User{}}

But I still have many, many, errors and warnings, as if it was basically not made to be doctested...

For example, the default generated doc puts:

iex> update_user(user, %{field: new_value})
{:ok, %User{}}

This would issue:
warning: variable "new_value" does not exist and is being expanded to "new_value()"...
warning: variable "user" does not exist and is being expanded to "user()"...
test/my_app/accounts_test.exs:45: undefined function new_value/0
test/my_app/accounts_test.exs:45: undefined function user/0

My question is: is there something I'm missing? Or is it generally not common to doctest one's context (which would explain why it doesn't work out-of-the-box)

Upvotes: 7

Views: 499

Answers (1)

José Valim
José Valim

Reputation: 51439

Generally speaking we do not doctest functions that have side-effects, such as context functions that have to read/write to the database, because they would require some amount of setup that is hard to cleanly portray in the doctest itself.

It is certainly doable but you should ask yourself if it is worth the effort. The Phoenix team itself seems to think it isn't. :)

Upvotes: 14

Related Questions