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

Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
asfand
I am Asfandyar from Pakistan. This is my first time to the forum. I develop PHP websites using CodeIgniter, which is super easy to learn...
New
wolfiton
Question: Can someone recommend me some good resources on learning performance for phoenix elixir applications and a design pattern I sh...
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
pdgonzalez872
Hi! In my quest of becoming the best Elixir dev I can be, I saw one aspect in my career that I’d like to improve upon. This is language...
New
stevensonmt
I’d like to provide my review of the Elixir Course module from Groxio. I have some criticisms but I’d like to start with the positives. ...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
QueenSvetlana
My bookstore has Introducing Elixir: Getting Started In Functional Programming and the front cover says the book uses Elixir 1.4. The cur...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New

Other popular topics Top

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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