Testing Update

Testing update is a bit of a combination of the testing for the create and read functions. Let’s first look at the function we’re going to test:

 def​ update(%User{} = existing_user, update_params) ​do
  existing_user
  |> User.update_changeset(update_params)
  |> Repo.update()
 end

You can see that, like create/1, this function is a very lightweight wrapper around calls to the schema’s changeset function (update_changeset/2 in this case) and then to Repo.update/1. Testing it will be similar, but you’ll need to insert an existing user to be updated by the code. Let’s write our success test first:

1: describe ​"​​update/2"​ ​do
test ​"​​success: it updates database and returns the user"​ ​ ...

Get Testing Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.