Skip to content

Commit 6c70374

Browse files
committed
add faker, add faker email, add test users
1 parent 5eaa953 commit 6c70374

File tree

5 files changed

+130
-3
lines changed

5 files changed

+130
-3
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ gem "tzinfo-data", platforms: %i[ windows jruby ]
2727
gem "solid_cache"
2828
gem "solid_queue"
2929
gem "solid_cable"
30-
30+
gem "faker"
3131
# Reduces boot times through caching; required in config/boot.rb
3232
gem "bootsnap", require: false
3333

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ GEM
116116
factory_bot_rails (6.4.4)
117117
factory_bot (~> 6.5)
118118
railties (>= 5.0.0)
119+
faker (3.5.1)
120+
i18n (>= 1.8.11, < 2)
119121
ffi (1.17.1-aarch64-linux-gnu)
120122
ffi (1.17.1-aarch64-linux-musl)
121123
ffi (1.17.1-arm-linux-gnu)
@@ -409,6 +411,7 @@ DEPENDENCIES
409411
capybara
410412
debug
411413
factory_bot_rails
414+
faker
412415
hotwire-spark
413416
importmap-rails
414417
jbuilder

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class User < ApplicationRecord
44

55
normalizes :email, with: ->(e) { e.strip.downcase }
66

7-
validates :email, presence: true, uniqueness: true
7+
validates :email, presence: true, uniqueness: true,format: URI::MailTo::EMAIL_REGEXP
88
validates :password_digest, presence: true
99
end

spec/factories/users.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FactoryBot.define do
22
factory :user do
3-
email { "[email protected]" }
3+
email { Faker::Internet.email}
44
password { "password" }
55
end
66
end

spec/requests/users_spec.rb

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
require 'rails_helper'
2+
3+
# This spec was generated by rspec-rails when you ran the scaffold generator.
4+
# It demonstrates how one might use RSpec to test the controller code that
5+
# was generated by Rails when you ran the scaffold generator.
6+
#
7+
# It assumes that the implementation code is generated by the rails scaffold
8+
# generator. If you are using any extension libraries to generate different
9+
# controller code, this generated spec may or may not pass.
10+
#
11+
# It only uses APIs available in rails and/or rspec-rails. There are a number
12+
# of tools you can use to make these specs even more expressive, but we're
13+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
14+
15+
RSpec.describe "/users", type: :request do
16+
let(:user) { create(:user) }
17+
18+
include AuthHelper
19+
20+
before do
21+
sign_in(user)
22+
end
23+
24+
# This should return the minimal set of attributes required to create a valid
25+
# User. As you add validations to User, be sure to
26+
# adjust the attributes here as well.
27+
let(:valid_attributes) { { email: "[email protected]" , password:"123"} }
28+
29+
let(:invalid_attributes) { { email: "" } }
30+
31+
describe "GET /index" do
32+
it "renders a successful response" do
33+
user = FactoryBot.create(:user)
34+
get users_url
35+
expect(response).to be_successful
36+
end
37+
end
38+
39+
describe "GET /new" do
40+
it "renders a successful response" do
41+
get new_user_url
42+
expect(response).to be_successful
43+
end
44+
end
45+
46+
describe "GET /edit" do
47+
it "renders a successful response" do
48+
user = FactoryBot.create(:user)
49+
get edit_user_url(user)
50+
expect(response).to be_successful
51+
end
52+
end
53+
54+
describe "POST /create" do
55+
context "with valid parameters" do
56+
it "creates a new user" do
57+
expect {
58+
post users_url, params: { user: valid_attributes }
59+
}.to change(User, :count).by(1)
60+
end
61+
62+
it "redirects to the user index" do
63+
post users_url, params: { user: valid_attributes }
64+
expect(response).to redirect_to(users_path)
65+
end
66+
end
67+
68+
context "with invalid parameters" do
69+
it "does not create a new User" do
70+
expect {
71+
post users_url, params: { user: invalid_attributes }
72+
}.to change(User, :count).by(0)
73+
end
74+
75+
it "renders a response with 422 status (i.e. to display the 'new' template)" do
76+
post users_url, params: { user: invalid_attributes }
77+
expect(response).to have_http_status(:unprocessable_entity)
78+
end
79+
end
80+
end
81+
82+
describe "PATCH /update" do
83+
context "with valid parameters" do
84+
let(:new_attributes) { { email: "[email protected]" } }
85+
86+
it "updates the requested user" do
87+
user = User.create! valid_attributes
88+
patch user_url(user), params: { user: new_attributes }
89+
user.reload
90+
expect(user.email).to eq("[email protected]")
91+
end
92+
93+
it "redirects to the user" do
94+
user = User.create! valid_attributes
95+
patch user_url(user), params: { user: new_attributes }
96+
user.reload
97+
expect(response).to redirect_to(users_url)
98+
end
99+
end
100+
101+
context "with invalid parameters" do
102+
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
103+
user = User.create! valid_attributes
104+
patch user_url(user), params: { user: invalid_attributes }
105+
expect(response).to have_http_status(:unprocessable_entity)
106+
end
107+
end
108+
end
109+
110+
describe "DELETE /destroy" do
111+
it "destroys the requested user" do
112+
user = User.create! valid_attributes
113+
expect {
114+
delete user_url(user)
115+
}.to change(User, :count).by(-1)
116+
end
117+
118+
it "redirects to the users list" do
119+
user = User.create! valid_attributes
120+
delete user_url(user)
121+
expect(response).to redirect_to(users_url)
122+
end
123+
end
124+
end

0 commit comments

Comments
 (0)