You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter03-presenting-users.adoc
+26-15Lines changed: 26 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ We will modify a little bit this migration in order to add some database validat
78
78
We will therefore add two additional constraints:
79
79
80
80
- email is mandatory: we use the property `null: false`.
81
-
- email must be unique: we use the `unique: true` property
81
+
- email must be unique: we add an index for the email column with property `unique: true`.
82
82
- password is mandatory: we use the property `null: false`.
83
83
84
84
The migration thus becomes:
@@ -88,7 +88,8 @@ The migration thus becomes:
88
88
----
89
89
# ...
90
90
create_table :users do |t|
91
-
t.string :email, unique: true, null: false
91
+
t.string :email, null: false
92
+
t.index :email, unique: true
92
93
t.string :password_digest, null: false
93
94
# ...
94
95
end
@@ -181,7 +182,7 @@ end
181
182
[source,ruby]
182
183
----
183
184
# ...
184
-
test 'user with unvalid email should be unvalid' do
185
+
test 'user with invalid email should be invalid' do
185
186
user = User.new(email: 'test', password_digest: 'test')
186
187
assert_not user.valid?
187
188
end
@@ -193,7 +194,7 @@ end
193
194
[source,ruby]
194
195
----
195
196
# ...
196
-
test 'user with taken email should be unvalid' do
197
+
test 'user with taken email should be invalid' do
197
198
other_user = users(:one)
198
199
user = User.new(email: other_user.email, password_digest: 'test')
199
200
assert_not user.valid?
@@ -267,7 +268,7 @@ end
267
268
268
269
In addition, this method will add a `User#password` attribute that will be automatically hashed and saved in the `User#password_digest` attribute.
269
270
270
-
Let's try this right now in the Rails console. Open a console with `console rails`:
271
+
Let's try this right now in the Rails console. Open a console with `rails console`:
271
272
272
273
[source,ruby]
273
274
----
@@ -342,7 +343,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
342
343
test "should show user" do
343
344
get api_v1_user_url(@user), as: :json
344
345
assert_response :success
345
-
# on teste que la réponse contient le courriel
346
+
# Test to ensure response contains the correct email
346
347
json_response = JSON.parse(self.response.body)
347
348
assert_equal @user.email, json_response['email']
348
349
end
@@ -356,6 +357,7 @@ Then simply add the action to our controller. It is extremely simple:
356
357
.app/controllers/api/v1/users\_controller.rb
357
358
----
358
359
class Api::V1::UsersController < ApplicationController
360
+
# GET /users/1
359
361
def show
360
362
render json: User.find(params[:id])
361
363
end
@@ -409,6 +411,14 @@ $ git add . && git commit -m "Adds show action the users controller"
409
411
410
412
So we finally have a resource to test. We have several solutions to test it. The first one that comes to mind is the use of cURL, which is integrated in almost all Linux distributions. So let's try it:
411
413
414
+
First initialize the rails server on a new terminal.
415
+
[source,bash]
416
+
----
417
+
$ rails s
418
+
----
419
+
420
+
Then switch back to your other terminal and run:
421
+
412
422
[source,bash]
413
423
----
414
424
$ curl http://localhost:3000/api/v1/users/1
@@ -449,8 +459,8 @@ end
449
459
450
460
That's a lot of code. Don't worry I'll explain everything:
451
461
452
-
* In the first test we check the creation of a user by sending a valid POST request. Then, we checked that an additional user exists in the database and that the HTTP code of the response is `created`
453
-
* In the first test we check that the user is not created using an email already used. Then, we check that the HTTP code of the response is `created`
462
+
* In the first test we check the creation of a user by sending a valid POST request. Then, we checked that an additional user exists in the database and that the HTTP code of the response is `created` (status code 201)
463
+
* In the second test we check that the user is not created using an email already used. Then, we check that the HTTP code of the response is `unprocessable_entity` (status code 422)
454
464
455
465
At that point, the tests must fail (as we expected):
456
466
@@ -538,7 +548,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
538
548
assert_response :success
539
549
end
540
550
541
-
test "should not update other user" do
551
+
test "should not update user when invalid params are sent" do
0 commit comments