wolfiton
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
Trending in Chat/Questions
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban).
For this kind of work you do not tend, in my e...
New
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
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
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 10 Posts
NobbZ
You have told us how the test looks like, but not the function under test, perhaps even the validations mihgt be of special interest here.
Also, if that book does it in a TDD manor, have you peeked at the next couple of pages if they do make the tests pass on the next couple of pages?
wolfiton
The book doesn’t do the tests in TDD so the accounts context and user changeset can be of interest here?
NobbZ
The tests are testing if names of certain lengths are invalid.
As the tests are currently failing because of a
MatchErrorwhere you call theAccounts.register_user/1with{:error, _}not matching some{:ok, _}, it is very likely that the length validations for the username are currently either implemented wrong or missing at all.wolfiton
Here is the registration_changeset
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.wolfiton
Let me share the whole schema because the schema has also name and username in it one sec.
Also the also the attrs are:
NobbZ
Yes, now I see it. In both cases
:usernameis set in the result. I just didn’t realize it in the mobile…In the
registration_changeset/1you do not have any verification for the:username, its only in thechangeset. Which one is called in theregister_user/1?wolfiton
Like this:
I think that the registration changeset gets all the
changesetvalues form the default chnageset and adds onlypassoword and password_hashtoregister_changeset.Also you are right I had a validate_length of 75 for username, changed it to 20.
Solved one error the username now passes the test
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: 6should solve the problemwolfiton
Thanks @archdragon.
That was it I haven’t verified the changeset validation so my assertion couldn’t match.
These lines were the problems
Replace this
with this
and this
with this
Now all tests are green and pass.
Thanks everyone for helping out