Iggy

Iggy

Testing Virtual Field

Hey all,

What is the best practice when testing user’s password? My user schema has a :password_digest field and :password virtual field. I am having problem making the test pass because password field is empty.

On sign up, user enters their password. In user changeset, the password is hashed (with bcrypt), and then that hash is saved under password_digest.

  schema "users" do
    field :email, :string
    field :password_digest, :string
    field :username, :string

    timestamps()

    field :password, :string, virtual: true
  end

In the test, I have (from generator, modified a bit):

@valid_attrs %{email: "some email", password: "some password", username: "some username"}

   ...

def user_fixture(attrs \\ %{}) do
  {:ok, user} =
    attrs
    |> Enum.into(@valid_attrs)
    |> Accounts.create_user()

  user
end

  ...

test "get_user!/1 returns the user with given id" do
  user = user_fixture()
  assert Accounts.get_user!(user.id) == user
end

The test above fails because password on left is nil, while on the right side it still has the password. I am not saving password in database.

  1) test users get_user!/1 returns the user with given id...
     ...
     Assertion with == failed
     code:  assert Accounts.get_user!(user.id()) == user
     left: ... email: "some email", id: 56, inserted_at: ~N[2018-11-22 16:30:48], password_digest: "$2b$04$qjv2Log0RIcFoMdrLEbGGeMzBr3JQ9epDNgjSkxSB3cQO5ZBuzKPS", updated_at: ~N[2018-11-22 16:30:48], username: "some username", password: nil}
     right: ... email: "some email", id: 56, inserted_at: ~N[2018-11-22 16:30:48], password_digest: "$2b$04$qjv2Log0RIcFoMdrLEbGGeMzBr3JQ9epDNgjSkxSB3cQO5ZBuzKPS", updated_at: ~N[2018-11-22 16:30:48], username: "some username", password: "some password_digest"}

user_fixture saves the password, but Accounts.get_user does not. Btw, get user is taken from generator: def get_user!(id), do: Repo.get!(User, id).

Thanks!

Most Liked

idi527

idi527

You can try testing the changeset function. Or write a helper that would populate user’s password manually before assert.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement