wolfiton
Programming phoenix 1.4 chapter 8 page 151
Hi everyone,
I don’t understand why this test is failing instead of going to the changeset and give an error and pass the test. Because that error was expected in that test.
Expected result:
To append an error to the changeset and pass the tests for username to long and password to short
Test parts that fail because the password and users don’t match
test "does not accept long usernames" do
attrs = Map.put(@valid_attrs, :username, String.duplicate("a", 30))
{:error, changeset} = Accounts.register_user(attrs)
assert %{username: ["should be at most 20 character(s)"]} = errors_on(changeset)
assert Accounts.list_users() == []
end
test "requires password to be at least 6 chars long" do
attrs = Map.put(@valid_attrs, :password, "12345")
{:error, changeset} = Accounts.register_user(attrs)
assert %{password: ["should be at least 6 character(s)"]} = errors_on(changeset)
assert Accounts.list_users() == []
end
Error trace:
rumbl mix test test/rumbl/accounts_test.exs
.
1) test register_user/1 does not accept long usernames (Rumbl.AccountsTest)
test/rumbl/accounts_test.exs:36
** (MatchError) no match of right hand side value: {:ok, %Rumbl.Accounts.User{__meta__:
#Ecto.Schema.Metadata<:loaded, "users">, id: 18, inserted_at: ~N[2020-01-19 15:34:04],
name: "User", password: "secret", password_hash: "$pbkdf2-sha512$160000$iahAZDIoM/oP/PA9AlAIXQ$mxd9aqd.QhF9MPPjhSOyu.QiMFIAdZkLUUXoRTbwpyoqB0V5af6FpfE/USCoAxMvC.HW1i1m.0B7o1JYHDAyDQ", updated_at: ~N[2020-01-19 15:34:04], username: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}}
code: {:error, changeset} = Accounts.register_user(attrs)
stacktrace:
test/rumbl/accounts_test.exs:38: (test)
2) test register_user/1 requires password to be at least 6 chars long (Rumbl.AccountsTest)
test/rumbl/accounts_test.exs:44
** (MatchError) no match of right hand side value: {:ok, %Rumbl.Accounts.User{__meta__:
#Ecto.Schema.Metadata<:loaded, "users">, id: 19, inserted_at: ~N[2020-01-19 15:34:05],
name: "User", password: "12345",
password_hash:
"$pbkdf2-sha512$160000$g5239LoShgVmsdO7qvYzVQ$cJnOY1lKgDjsAYT7Svi2P58qZly0khxmhEwOMfYk6mELoOtxlKUe4YMC0AE8vf1.m7J0M8EdlQOsCly0hyGy7g",
updated_at: ~N[2020-01-19 15:34:05], username: "eva"}}
code: {:error, changeset} = Accounts.register_user(attrs)
stacktrace:
test/rumbl/accounts_test.exs:46: (test)
..
Finished in 5.5 seconds
5 tests, 2 failures
Randomized with seed 829346
Also the full test can be seen here
Thanks in advance
Most Liked
NobbZ
Okay, take a look at your changeset. It does not care for the name at all.
Also somewhere inbetween, there seems to happen something else, as the resulting struct has name: "User" in both cases, while you pass in your wrong name as :username.
NobbZ
Yes, now I see it. In both cases :username is set in the result. I just didn’t realize it in the mobile…
In the registration_changeset/1 you do not have any verification for the :username, its only in the changeset. Which one is called in the register_user/1?
archdragon
I think the problem here is much simpler.
In the test you write
but in the actual validation only 5 characters are required.
This is why password: "12345" returns {:ok, _} instead of an error.
Changing the validation to min: 6 should solve the problem ![]()
Popular in Chat/Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








