Pattern matching a struct *type* in a doctest

I wrote a doctest that tests that the function is returning a %Foobar.Accounts.User struct type.

  1) test doc at Foobar.Accounts.create_user_with_email_only/1 (1) (Foobar.AccountsTest)
     Doctest failed
     code: Foobar.Accounts.create_user_with_email_only("") === {:ok, %Foobar.Accounts.User{}}
     left: {:ok,
            %Foobar.Accounts.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
             confirmation_code: "338128", email: "",
             email_confirmed: false, encrypted_password: nil, fullname: nil,
             id: 2306, inserted_at: ~N[2017-07-25 03:26:15.407053],
             invite_token: nil, onboarded: false, password: nil, pin: nil,
             roles: nil,
             team: #Ecto.Association.NotLoaded<association :team is not loaded>,
             team_id: nil, token: "flDJRP6Vx78pI3uSOgSVPb2HxcEZ3DYU",
             updated_at: ~N[2017-07-25 03:26:15.413361], username: nil}}
       lib/accounts/accounts.ex:15: Foobar.Accounts (module)

My doctests look like:

@doc """
Create a user with only the email field set. The user is not
associated with any team.

## Examples

    iex> Foobar.Accounts.create_user_with_email_only("")
    {:ok, %Foobar.Accounts.User{}}

    iex> Foobar.Accounts.create_user_with_email_only("")
    {:error, "Email cannot be blank or nil."}

    iex> Foobar.Accounts.create_user_with_email_only(nil)
    {:error, "Email cannot be blank or nil."}

def create_user_with_email_only(email) when byte_size(email) > 0 do

Thanks for the help!

Doctest does not pattern match result, it explicitly compares it against pinned value:

iex|1 ▶ map = %{foo: 42}
%{foo: 42}
iex|2 ▶ case %{foo: 42, bar: 3.14} do
...|2 ▶ ^map -> :matched
...|2 ▶ _ -> :not_matched
...|2 ▶ end

That said, you should retrieve the result and assert the explicit pattern match:

iex|3 ▶ result = Foobar.Accounts.create_user_with_email_only("")
...|3 ▶ with {:ok, %Foobar.Accounts.User{}} <- result, do: :passed

Another idea similar to the above without the use of with. But I really like the idea with with too :slight_smile:

iex> {status, %Foobar.Accounts.User{}} = Foobar.Accounts.create_user_with_email_only("")
....> status

This has a drawback: is the test fails, there would be a messy message; with-approach will respond with smth like “expected :passed, got …” putting out to the screen the actual result (due to how with works.)

Ah, I see your point. And the idea with with is not really much complex or long. So yeah the with idea is better.