wolfiton

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

http://media.pragprog.com/titles/phoenix14/code/testing_mvc/listings/rumbl/test/rumbl/accounts_test.exs

Thanks in advance

Most Liked

NobbZ

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

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

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 :slight_smile:

wolfiton

wolfiton

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

|> validate_length(:username, min: 1, max: 75)

with this

|> validate_length(:username, min: 1, max: 20)`

and this

|> validate_length(:password, min: 5, max: 100)

with this

|> validate_length(:password, min: 6, max: 20)

Now all tests are green and pass.

Thanks everyone for helping out

Where Next?

Popular in Chat/Questions Top

wolfiton
Hi everyone, How can i retrieve the name from a structure like this? %{"id" =&gt; "1570", "name" =&gt; "Croque Monsieur"} My test loo...
New
Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
loganhelms
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
New
wolfiton
Question: Can someone recommend me some good resources on learning performance for phoenix elixir applications and a design pattern I sh...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
davearonson
I am looking for smallish problems to solve, using Elixir concepts beyond the basic syntax and concepts of Elixir as a language, such as ...
New
TwistingTwists
I want to learn DSL. Don’t know how to write one. What;s the best introductory resource? I see some macro being used here. Is DSL only ...
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
Chawki
Hi,i’m new to elixir. i’m searching elixir small programs to try it out my self,Is any good resources out there? Thank you.
New
dogweather
Can anyone recommend books/courses/videos that use real-world Elixir? E.g.: Idiomatic error handling design, whether it’s {ok/error, .....
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement