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:

Where Next?

Popular in Chat/Questions Top

roshan
Hi everyone, I’m looking for a book on Phoenix server hosting / deployment like the following books for Rails, Docker for Rails Develop...
New
Allyedge
Hey, I want to learn Elixir OTP and I wanted to know if there are any good resources that teach it. I found some web pages, but none of t...
New
dopomecana
The title of this topic may sound silly at the first glance. But why I trying to ask you guys is how to go to the next level? I am curre...
New
yachnytskyi
Hello everyone. I am gonna start with Elixir/Phoenix, thinking to use Stephen Grider as a start point, then elixir school and other sourc...
New
aswinmohanme
I recently finished the Udemy course on Elixir and Phoenix and I am thinking about using it for the next project. But I am stuck as how t...
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
eliottramirez
Hello, I’m trying to learn Phoenix but I constantly find difficult understanding how the framework works, and I think part of this is th...
New
lc0815
hello from a real frustrated newbe… I’m reading this article Full-Stack React With Phoenix (Chapter 3 | Introduction to Phoenix) by mich...
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement